Geometrize  1.0
An application for geometrizing images into geometric primitives
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
Functions
geometrize::util Namespace Reference

Functions

void debugBreak ()
 debugBreak Forces the debugger to break/halt the application when this is called. More...
 
void printToConsole (const std::string &str)
 printToConsole A convenience function for printing a string to the command-line console. More...
 
void printToAllScriptConsoleWidgets (const std::string &str)
 printToAllScriptConsoleWidgets A convenience function for printing a string to all the GUI console widgets in the application More...
 
void messageBox (const std::string &str)
 messageBox A convenience function for displaying a message box containing a message. More...
 
void processApplicationEvents ()
 processApplicationEvents Processes all pending application events for the calling thread. More...
 
bool fileExists (const std::string &filePath)
 fileExists Checks if a file exists, returns true if it does. More...
 
bool directoryExists (const std::string &dirPath)
 directoryExists Checks if a directory exists, returns true if it does. More...
 
bool directoryContainsFile (const std::string &dirPath, const std::string &fileName)
 directoryContainsFile Checks if the directory contains the given file, returns true if it does. More...
 
std::string readFileAsString (const std::string &filePath)
 readFileAsString Reads a file, returning a string containing the contents. More...
 
std::vector< std::string > getFilePathsForDirectory (const std::string &dirPath)
 getFilePathsForDirectory Gets the absolute file paths to the files in the given directory. More...
 
std::vector< std::string > getSubdirectoriesForDirectory (const std::string &dirPath)
 getSubdirectoriesForDirectory Gets the absolute paths for direct subdirectories of the given directory. More...
 
bool endsWith (const std::string &value, const std::string &ending)
 
std::vector< std::string > getFilesWithExtension (const std::string &dirPath, const std::string &extension)
 getFilesWithExtension Gets the absolute file paths to the files with the given extension in the given directory. More...
 
std::string getFirstFileWithExtension (const std::string &dirPath, const std::string &extension)
 getFirstFileWithExtension Gets the absolute file path to the first file with the given extension in the given directory. More...
 
std::string getFirstFileWithExtensions (const std::string &dirPath, const std::vector< std::string > &extensions)
 getFirstFileWithExtensions Gets the absolute file path to the first file with one of the given extensions in the given directory. More...
 
std::vector< std::string > getScriptsForPath (const std::string &path)
 getScriptsForPath Gets the absolute file paths to the script files (*.chai) for the given directory, using recursive search. More...
 
std::string getDirectoryForFilePath (const std::string &filePath)
 getDirectoryForFilePath Gets the directory path from the given local file path. More...
 
bool openInDefaultApplication (const std::string &path)
 openInDefaultApplication Opens the given path in the default application. More...
 
bool revealInDefaultApplication (const std::string &path)
 revealInDefaultApplication Reveals the given path in the parent folder (or path with the filename trimmed). More...
 
void clearGlobalClipboard ()
 clearGlobalClipboard Clears the global system clipboard contents. More...
 
std::string getGlobalClipboardText ()
 getGlobalClipboardText Gets the global system clipboard text. More...
 
void setGlobalClipboardText (const std::string &text)
 setClipboardText Sets the global system clipboard text. More...
 
bool stringBeginsWith (const std::string &str, const std::string &prefix)
 stringBeginsWith Checks if a string begins with the given prefix. More...
 
bool stringEndsWith (const std::string &str, const std::string &suffix)
 stringEndsWith Checks if a string ends with the given suffix. More...
 
std::string getApplicationDirectoryLocation ()
 getApplicationDirectory Get directory that the application is in. More...
 
std::string getAppDataLocation ()
 getAppDataLocation Returns a directory location where persistent application data can be stored. More...
 
std::string getHomeDirectoryLocation ()
 getHomeDirectoryLocation Returns a path to where the user's home directory is. More...
 
bool writeStringToFile (const std::string &str, const std::string &path)
 writeStringToFile Writes the string to the file path, attempting to overwrite any existing file at the path. More...
 
std::string percentEncode (const std::string &str)
 percentEncode Returns a percent-encoded copy of the input string. More...
 
int randomInRange (int lower, int upper)
 randomInRange Returns a random integer in the range [lower, upper], inclusive. Note that this uses thread-local RNGs under the hood. This must not be called on the same thread as the Geometrize shape mutation code - otherwise the shape generation may not be deterministic. More...
 
std::vector< std::string > split (const std::string &s, char delimiter)
 split Splits a given string into a vector of tokens using the given delimiter. More...
 
int getCursorX ()
 getCursorX Gets the x-coordinate of the cursor in global screen coordinates. More...
 
int getCursorY ()
 getCursorY Gets the y-coordinate of the cursor in global screen coordinates. More...
 
void setCursorPos (int x, int y)
 setCursorPos Sets the cursor position in global screen coordinates. More...
 
std::string getOperatingSystemProductType ()
 getOperatingSystemProductType Returns the product name of the operating system this application is running in. More...
 
bool saveDesktopScreenshot (const std::string &path)
 saveDesktopScreenshot Takes a screenshot of the desktop and saves it as a .png image to the given path. More...
 
bool saveWidgetScreenshot (const std::string &path, QWidget *widget)
 saveWidgetScreenshot Takes a screenshot of the given widget and saves it as a .png image to the given path. More...
 
template<typename T >
clamp (T value, T lower, T upper)
 clamp Clamps a value within a range. More...
 
template<typename T >
void split (const std::string &s, const char delimiter, T result)
 
std::vector< std::string > readStringVector (const std::string &filePath)
 readStringVector Reads the JSON file containing the vector of strings at the given filepath. Will attempt to overwrite any existing file when it does this. More...
 
void writeStringVector (const std::vector< std::string > &vec, const std::string &filePath)
 writeStringVector Writes the string vector to the given filepath as JSON. Will attempt to overwrite any existing file. More...
 
void openTask (const QString &urlStr, bool addToRecents)
 
void openTasks (const QStringList &urls, const bool addToRecents)
 
bool openTemplate (chaiscript::ChaiScript &engine, const std::string &templateFolder)
 
TemplateManifest getTemplateManifest (const std::string &templateFolder)
 

Function Documentation

template<typename T >
T geometrize::util::clamp ( value,
lower,
upper 
)

clamp Clamps a value within a range.

Parameters
valueThe value to clamp.
lowerThe lower bound of the range.
upperThe upper bound of the range.
Returns
The clamped value.
221 {
222  assert(lower <= upper);
223  if (value < lower) {
224  value = lower;
225  }
226  if (value > upper) {
227  value = upper;
228  }
229  return value;
230 }

Here is the caller graph for this function:

void geometrize::util::clearGlobalClipboard ( )

clearGlobalClipboard Clears the global system clipboard contents.

232 {
233  QApplication::clipboard()->clear();
234 }

Here is the caller graph for this function:

void geometrize::util::debugBreak ( )

debugBreak Forces the debugger to break/halt the application when this is called.

43 {
44 #ifdef _MSC_VER
45  __debugbreak();
46 #elif __MINGW32__
47  // Not sure how to break here...
48  assert(0 && "Mingw32 debug break");
49 #else
50  raise(SIGTRAP);
51 #endif
52 }

Here is the caller graph for this function:

bool geometrize::util::directoryContainsFile ( const std::string &  dirPath,
const std::string &  fileName 
)

directoryContainsFile Checks if the directory contains the given file, returns true if it does.

Returns
True if the directory contains the given file, false otherwise.
98 {
99  const QString qFileName{QString::fromStdString(fileName)};
100  QDirIterator it(QString::fromStdString(dirPath));
101  while(it.hasNext()) {
102  it.next();
103  if(QFileInfo(it.filePath()).isFile()) {
104  if(it.fileName() == qFileName) {
105  return true;
106  }
107  }
108  }
109  return false;
110 }

Here is the caller graph for this function:

bool geometrize::util::directoryExists ( const std::string &  dirPath)

directoryExists Checks if a directory exists, returns true if it does.

Parameters
dirPathThe directory path.
Returns
True if the directory exists, false if it does not.
93 {
94  return QDir(QString::fromStdString(dirPath)).exists();
95 }

Here is the caller graph for this function:

bool geometrize::util::endsWith ( const std::string &  value,
const std::string &  ending 
)
inline
152 {
153  if (value.length() >= ending.length()) {
154  return (0 == value.compare (value.length() - ending.length(), ending.length(), ending));
155  } else {
156  return false;
157  }
158 }

Here is the caller graph for this function:

bool geometrize::util::fileExists ( const std::string &  filePath)

fileExists Checks if a file exists, returns true if it does.

Parameters
filePathThe file path.
Returns
True if the file exists, false if it does not.
88 {
89  return QFile(QString::fromStdString(filePath)).exists();
90 }

Here is the caller graph for this function:

std::string geometrize::util::getAppDataLocation ( )

getAppDataLocation Returns a directory location where persistent application data can be stored.

Returns
Directory location where persistent application data can be stored.
262 {
263  return QStandardPaths::writableLocation(QStandardPaths::DataLocation).toStdString();
264 }

Here is the caller graph for this function:

std::string geometrize::util::getApplicationDirectoryLocation ( )

getApplicationDirectory Get directory that the application is in.

Returns
Get application directory.
257 {
258  return QCoreApplication::applicationDirPath().toStdString();
259 }

Here is the caller graph for this function:

int geometrize::util::getCursorX ( )

getCursorX Gets the x-coordinate of the cursor in global screen coordinates.

Returns
The x-coordinate of the cursor.
312 {
313  return QCursor::pos().x();
314 }

Here is the caller graph for this function:

int geometrize::util::getCursorY ( )

getCursorY Gets the y-coordinate of the cursor in global screen coordinates.

Returns
The y-coordinate of the cursor.
317 {
318  return QCursor::pos().y();
319 }

Here is the caller graph for this function:

std::string geometrize::util::getDirectoryForFilePath ( const std::string &  filePath)

getDirectoryForFilePath Gets the directory path from the given local file path.

Parameters
filePathThe filepath to chop.
Returns
The directory path i.e. the file path with the file name and any trailing slashes removed.
210 {
211  const QFileInfo fileInfo{QString::fromStdString(filePath)};
212 
213  if(!fileInfo.exists()) {
214  assert(0 && "File does not exist");
215  }
216 
217  return fileInfo.absoluteDir().absolutePath().toStdString();
218 }
std::vector< std::string > geometrize::util::getFilePathsForDirectory ( const std::string &  dirPath)

getFilePathsForDirectory Gets the absolute file paths to the files in the given directory.

Parameters
dirPathThe directory to search.
Returns
A vector of files.
124 {
125  std::vector<std::string> files;
126 
127  QDirIterator it(QString::fromStdString(dirPath));
128  while(it.hasNext()) {
129  it.next();
130  if(QFileInfo(it.filePath()).isFile()) {
131  files.push_back(it.filePath().toStdString());
132  }
133  }
134  return files;
135 }

Here is the caller graph for this function:

std::vector< std::string > geometrize::util::getFilesWithExtension ( const std::string &  dirPath,
const std::string &  extension 
)

getFilesWithExtension Gets the absolute file paths to the files with the given extension in the given directory.

Parameters
dirPathThe directory to search.
extensionThe file extension to match.
Returns
The file paths to the files encountered, empty vector if there are no matching files. The order of the file paths is implementation-dependent.
161 {
162  std::vector<std::string> files{getFilePathsForDirectory(dirPath)};
163 
164  files.erase(std::remove_if(files.begin(), files.end(), [&extension](const std::string& s) {
165  return !endsWith(s, extension);
166  }), files.end());
167 
168  return files;
169 }
bool endsWith(const std::string &value, const std::string &ending)
Definition: util.cpp:151
std::vector< std::string > getFilePathsForDirectory(const std::string &dirPath)
getFilePathsForDirectory Gets the absolute file paths to the files in the given directory.
Definition: util.cpp:123

Here is the call graph for this function:

Here is the caller graph for this function:

std::string geometrize::util::getFirstFileWithExtension ( const std::string &  dirPath,
const std::string &  extension 
)

getFirstFileWithExtension Gets the absolute file path to the first file with the given extension in the given directory.

Parameters
dirPathThe directory to search.
extensionThe file extension to match.
Returns
The file path to the first matching file encountered, empty string if there are no matching files. The 'first' file is implementation-dependent.
172 {
173  const std::vector<std::string> files{getFilesWithExtension(dirPath, extension)};
174 
175  if(files.empty()) {
176  return "";
177  }
178  return files.front();
179 }
std::vector< std::string > getFilesWithExtension(const std::string &dirPath, const std::string &extension)
getFilesWithExtension Gets the absolute file paths to the files with the given extension in the given...
Definition: util.cpp:160

Here is the call graph for this function:

Here is the caller graph for this function:

std::string geometrize::util::getFirstFileWithExtensions ( const std::string &  dirPath,
const std::vector< std::string > &  extensions 
)

getFirstFileWithExtensions Gets the absolute file path to the first file with one of the given extensions in the given directory.

Parameters
dirPathThe directory to search.
extensionsThe file extension to match.
Returns
The file path to the first matching file encountered, empty string if there are no matching files. The 'first' file is implementation-dependent.
182 {
183  for(const std::string& ext : extensions) {
184  const std::string s{getFirstFileWithExtension(dirPath, ext)};
185  if(!s.empty()) {
186  return s;
187  }
188  }
189 
190  return "";
191 }
std::string getFirstFileWithExtension(const std::string &dirPath, const std::string &extension)
getFirstFileWithExtension Gets the absolute file path to the first file with the given extension in t...
Definition: util.cpp:171

Here is the call graph for this function:

Here is the caller graph for this function:

std::string geometrize::util::getGlobalClipboardText ( )

getGlobalClipboardText Gets the global system clipboard text.

Returns
The global system clipboard text, or an empty string if the clipboard does not contain any text.
237 {
238  return QApplication::clipboard()->text().toStdString();
239 }

Here is the caller graph for this function:

std::string geometrize::util::getHomeDirectoryLocation ( )

getHomeDirectoryLocation Returns a path to where the user's home directory is.

Returns
Location of the user's home directory.
267 {
268  return QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString();
269 }

Here is the caller graph for this function:

std::string geometrize::util::getOperatingSystemProductType ( )

getOperatingSystemProductType Returns the product name of the operating system this application is running in.

Returns
The name of the operating system: "windows", "osx", "linux", "android", "ios", "tvos" etc
327 {
328  return QSysInfo::productType().toStdString();
329 }

Here is the caller graph for this function:

std::vector< std::string > geometrize::util::getScriptsForPath ( const std::string &  path)

getScriptsForPath Gets the absolute file paths to the script files (*.chai) for the given directory, using recursive search.

Parameters
dirPathThe directory to search.
Returns
The absolute file paths to the script files for the given directory.
194 {
195  std::vector<std::string> scripts;
196  if(!util::directoryExists(dirPath)) {
197  assert(0 && "Script directory does not exist");
198  return scripts;
199  }
200 
201  QDirIterator it(QString::fromStdString(dirPath), { "*.chai" }, QDir::Files, QDirIterator::Subdirectories);
202  while (it.hasNext()) {
203  scripts.push_back(it.next().toStdString());
204  }
205 
206  return scripts;
207 }
bool directoryExists(const std::string &dirPath)
directoryExists Checks if a directory exists, returns true if it does.
Definition: util.cpp:92

Here is the call graph for this function:

Here is the caller graph for this function:

std::vector< std::string > geometrize::util::getSubdirectoriesForDirectory ( const std::string &  dirPath)

getSubdirectoriesForDirectory Gets the absolute paths for direct subdirectories of the given directory.

Parameters
dirPathThe directory to search.
Returns
A vector of directory paths.
138 {
139  std::vector<std::string> files;
140 
141  QDirIterator it(QString::fromStdString(dirPath));
142  while(it.hasNext()) {
143  it.next();
144  if(QFileInfo(it.filePath()).isDir()) {
145  files.push_back(it.filePath().toStdString());
146  }
147  }
148  return files;
149 }

Here is the caller graph for this function:

TemplateManifest geometrize::util::getTemplateManifest ( const std::string &  templateFolder)
94 {
96 }
std::vector< std::string > getSupportedTemplateManifestFileExtensions()
getSupportedTemplateManifestFileExtensions Gets the supported template manifest file extensions for s...
Definition: formatsupport.cpp:28
std::string getFirstFileWithExtensions(const std::string &dirPath, const std::vector< std::string > &extensions)
getFirstFileWithExtensions Gets the absolute file path to the first file with one of the given extens...
Definition: util.cpp:181

Here is the call graph for this function:

Here is the caller graph for this function:

void geometrize::util::messageBox ( const std::string &  str)

messageBox A convenience function for displaying a message box containing a message.

Parameters
strThe string to display in the message box.
71 {
72  QMessageBox msgBox;
73  msgBox.setText(QString::fromStdString(str));
74  msgBox.exec();
75 }

Here is the caller graph for this function:

bool geometrize::util::openInDefaultApplication ( const std::string &  path)

openInDefaultApplication Opens the given path in the default application.

Parameters
pathThe path to the resource to open.
Returns
True if the directory, file, URL (or whatever) should open, else false.
221 {
222  return QDesktopServices::openUrl(QUrl::fromUserInput(QString::fromStdString(path)));
223 }

Here is the caller graph for this function:

void geometrize::util::openTask ( const QString &  urlStr,
bool  addToRecents 
)
30 {
31  if(urlStr.length() == 0) {
32  return;
33  }
34 
35  if(addToRecents) {
36  geometrize::getRecentItems().add(urlStr, urlStr);
37  }
38 
39  const QUrl url{QUrl::fromUserInput(urlStr)};
40  if(url.isLocalFile()) {
41  geometrize::task::createImageTaskAndWindow(url.toLocalFile().toStdString(), url.toLocalFile().toStdString());
42  return;
43  }
44 
45  std::vector<std::string> imageExtensions{format::getReadableImageFileExtensions(true)};
46  for(const std::string& ext : imageExtensions) {
47  if(url.toString().endsWith(QString::fromStdString(ext), Qt::CaseInsensitive)) {
49  return;
50  }
51  }
52 
54 }
void onImageDownloadComplete(network::Downloader *self, const QNetworkReply::NetworkError error)
onImageDownloadComplete A callback when an image download completes.
Definition: completionhandlers.cpp:24
ImageTask * createImageTaskAndWindow(const std::string &displayName, const std::string &taskUrl)
createImageTaskAndWindow Creates an image task, and immediately creates a graphical window for manipu...
Definition: imagetaskcreator.cpp:50
RecentItems & getRecentItems()
getRecentItems Gets a reference to the recent files list.
Definition: recentitems.cpp:206
void add(const QString &itemId, const QString &itemDisplayName)
add Adds an item to the recent items.
Definition: recentitems.cpp:177
void downloadImage(const QUrl &url, const std::function< void(network::Downloader *self, QNetworkReply::NetworkError error)> &onComplete)
downloadImage Downloads an image over the network.
Definition: networkactions.cpp:13
std::vector< std::string > getReadableImageFileExtensions(const bool includeDotPrefix)
getReadableImageFileExtensions Gets the lower-case image file extensions that have read support...
Definition: formatsupport.cpp:11
void downloadWebpage(const QUrl &url, const std::function< void(network::Downloader *self, QNetworkReply::NetworkError error)> &onComplete)
downloadWebpage Downloads a whole webpage over the network.
Definition: networkactions.cpp:18
void onWebpageDownloadComplete(network::Downloader *self, const QNetworkReply::NetworkError error)
onWebpageDownloadComplete A callback when a webpage download completes.
Definition: completionhandlers.cpp:51

Here is the call graph for this function:

Here is the caller graph for this function:

void geometrize::util::openTasks ( const QStringList &  urls,
const bool  addToRecents 
)
57 {
58  for(const QString& url : urls) {
59  openTask(url, addToRecents);
60  }
61 }
void openTask(const QString &urlStr, bool addToRecents)
Definition: taskutil.cpp:29

Here is the call graph for this function:

Here is the caller graph for this function:

bool geometrize::util::openTemplate ( chaiscript::ChaiScript &  engine,
const std::string &  templateFolder 
)
64 {
65  const std::vector<std::string> scripts{util::getScriptsForPath(templateFolder)};
66  if(scripts.empty()) {
67  // Could not find script for template, so assume it's an image task
68  const std::string imageFile{util::getFirstFileWithExtensions(templateFolder, format::getReadableImageFileExtensions(true))};
69  if(imageFile.empty()) {
70  return false;
71  }
72 
74 
75  // Apply settings file if a default one is available
76  const std::string settingsFile{geometrize::searchpaths::getDefaultTaskSettingsFilename()};
77  if(util::directoryContainsFile(templateFolder, settingsFile)) {
78  const std::string settingsPath{QDir(QString::fromStdString(templateFolder)).filePath(QString::fromStdString(settingsFile)).toStdString()};
79  const preferences::ImageTaskPreferences prefs(settingsPath);
80  task->setPreferences(prefs);
81  }
82 
83  return true;
84  }
85 
86  const std::string script{util::readFileAsString(scripts.front())};
87  engine.set_global(chaiscript::var(templateFolder), "templateDirectory");
88  geometrize::script::runScript(script, engine);
89 
90  return true;
91 }
ImageTask * createImageTaskAndWindow(const std::string &displayName, const std::string &taskUrl)
createImageTaskAndWindow Creates an image task, and immediately creates a graphical window for manipu...
Definition: imagetaskcreator.cpp:50
std::string getFirstFileWithExtensions(const std::string &dirPath, const std::vector< std::string > &extensions)
getFirstFileWithExtensions Gets the absolute file path to the first file with one of the given extens...
Definition: util.cpp:181
std::string readFileAsString(const std::string &filePath)
readFileAsString Reads a file, returning a string containing the contents.
Definition: util.cpp:112
std::vector< std::string > getScriptsForPath(const std::string &dirPath)
getScriptsForPath Gets the absolute file paths to the script files (*.chai) for the given directory...
Definition: util.cpp:193
std::vector< std::string > getReadableImageFileExtensions(const bool includeDotPrefix)
getReadableImageFileExtensions Gets the lower-case image file extensions that have read support...
Definition: formatsupport.cpp:11
bool directoryContainsFile(const std::string &dirPath, const std::string &fileName)
directoryContainsFile Checks if the directory contains the given file, returns true if it does...
Definition: util.cpp:97
std::string getDefaultTaskSettingsFilename()
getTaskSettingsFilename Gets the default name of a task settings file that is manually saved by the u...
Definition: searchpaths.cpp:31
void runScript(const std::string &code, chaiscript::ChaiScript &runner)
runScript Evaluates the provided script code.
Definition: scriptrunner.cpp:16
The ImageTask class transforms a source image into a collection of shapes approximating the source im...
Definition: imagetask.h:50

Here is the call graph for this function:

Here is the caller graph for this function:

std::string geometrize::util::percentEncode ( const std::string &  str)

percentEncode Returns a percent-encoded copy of the input string.

Parameters
strThe input string, to be percent-encoded.
Returns
The percent-encoded input string.
292 {
293  const QString s{QString::fromStdString(str)};
294  const QString percentEncoded{QUrl::toPercentEncoding(s)};
295  return percentEncoded.toStdString();
296 }

Here is the caller graph for this function:

void geometrize::util::printToAllScriptConsoleWidgets ( const std::string &  str)

printToAllScriptConsoleWidgets A convenience function for printing a string to all the GUI console widgets in the application

Parameters
strThe string to print to the script console widgets.
60 {
61  const auto& allWidgets = QApplication::topLevelWidgets();
62  for (const auto& widget : allWidgets) {
63  const auto& consoles = widget->findChildren<geometrize::dialog::ScriptConsole*>();
64  for(auto& console : consoles) {
65  console->appendString(str);
66  }
67  }
68 }
void appendString(const std::string &str)
appendString Appends a string to the console output window
Definition: scriptconsole.cpp:137
The ScriptConsole class models a command line and output window.
Definition: scriptconsole.h:25

Here is the call graph for this function:

Here is the caller graph for this function:

void geometrize::util::printToConsole ( const std::string &  str)

printToConsole A convenience function for printing a string to the command-line console.

Parameters
strThe string to print to the console.
55 {
56  QTextStream(stdout) << QString::fromStdString(str);
57 }

Here is the caller graph for this function:

void geometrize::util::processApplicationEvents ( )

processApplicationEvents Processes all pending application events for the calling thread.

78 {
79  QCoreApplication* app = QCoreApplication::instance();
80  if(app == nullptr) {
81  assert(0 && "Application instance pointer is null");
82  return;
83  }
84  app->processEvents();
85 }

Here is the caller graph for this function:

int geometrize::util::randomInRange ( int  lower,
int  upper 
)

randomInRange Returns a random integer in the range [lower, upper], inclusive. Note that this uses thread-local RNGs under the hood. This must not be called on the same thread as the Geometrize shape mutation code - otherwise the shape generation may not be deterministic.

Parameters
lowerThe minimum value.
upperThe maximum value.
Returns
An integer in the range [lower, upper] inclusive.
299 {
300  assert(lower <= upper);
301  return geometrize::commonutil::randomRange(lower, upper);
302 }

Here is the caller graph for this function:

std::string geometrize::util::readFileAsString ( const std::string &  filePath)

readFileAsString Reads a file, returning a string containing the contents.

Parameters
filePathThe path to the file.
Returns
The contents of the file, as a string.
113 {
114  QFile file(QString::fromStdString(filePath));
115 
116  if(file.exists() && file.open(QIODevice::ReadOnly | QIODevice::Text)) {
117  return QString(file.readAll()).toStdString();
118  }
119 
120  return "";
121 }

Here is the caller graph for this function:

std::vector< std::string > geometrize::util::readStringVector ( const std::string &  filePath)

readStringVector Reads the JSON file containing the vector of strings at the given filepath. Will attempt to overwrite any existing file when it does this.

Parameters
filePathThe path to the file to read.
Returns
The vector of strings read from the file.
21 {
22  if(!geometrize::util::stringEndsWith(filePath, ".json")) {
23  assert(0 && "Failed to read string vector, check file extension");
24  }
25 
26  std::vector<std::string> result;
27  std::ifstream file(filePath);
28  if(!file) {
29  return result;
30  }
31 
32  cereal::JSONInputArchive archive(file);
33  archive(result);
34  return result;
35 }
bool stringEndsWith(const std::string &str, const std::string &suffix)
stringEndsWith Checks if a string ends with the given suffix.
Definition: util.cpp:251

Here is the call graph for this function:

Here is the caller graph for this function:

bool geometrize::util::revealInDefaultApplication ( const std::string &  path)

revealInDefaultApplication Reveals the given path in the parent folder (or path with the filename trimmed).

Parameters
pathThe path to the resource to view.
Returns
True if the file, folder, URL (or whatever) should be revealed, else false.
226 {
227  const QUrl directoryPath{QUrl::fromUserInput(QString::fromStdString(path)).adjusted(QUrl::RemoveFilename)};
228  return QDesktopServices::openUrl(directoryPath);
229 }

Here is the caller graph for this function:

bool geometrize::util::saveDesktopScreenshot ( const std::string &  path)

saveDesktopScreenshot Takes a screenshot of the desktop and saves it as a .png image to the given path.

Parameters
pathThe file path to save the screenshot to.
Returns
True if the screenshot was taken and saved successfully, else false.
332 {
333  auto desktop = QApplication::desktop();
334  if(!desktop) {
335  assert(0 && "Failed to get desktop widget");
336  return false;
337  }
338 
339  const QPixmap desktopPixmap = desktop->window()->grab();
340 
341  return desktopPixmap.save(QString::fromStdString(path), "png");
342 }

Here is the caller graph for this function:

bool geometrize::util::saveWidgetScreenshot ( const std::string &  path,
QWidget *  widget 
)

saveWidgetScreenshot Takes a screenshot of the given widget and saves it as a .png image to the given path.

Parameters
pathThe file path to save the screenshot to.
widgetThe widget to take a screenshot of.
Returns
True if the screenshot was taken and save successfully, else false.
345 {
346  return widget->grab().save(QString::fromStdString(path), "png");
347 }

Here is the caller graph for this function:

void geometrize::util::setCursorPos ( int  x,
int  y 
)

setCursorPos Sets the cursor position in global screen coordinates.

Parameters
xThe x-coordinate of the cursor.
yThe y-coordinate of the cursor.
322 {
323  QCursor::setPos(x, y);
324 }

Here is the caller graph for this function:

void geometrize::util::setGlobalClipboardText ( const std::string &  text)

setClipboardText Sets the global system clipboard text.

Parameters
textThe text to set as the clipboard text.
242 {
243  QApplication::clipboard()->setText(QString::fromStdString(text));
244 }

Here is the caller graph for this function:

template<typename T >
void geometrize::util::split ( const std::string &  s,
const char  delimiter,
result 
)
234 {
235  std::stringstream ss;
236  ss.str(s);
237  std::string item;
238  while (std::getline(ss, item, delimiter)) {
239  *(result++) = item;
240  }
241 }
std::vector< std::string > geometrize::util::split ( const std::string &  s,
char  delimiter 
)

split Splits a given string into a vector of tokens using the given delimiter.

Parameters
sThe string to split.
delimiterThe delimiter to split on.
Returns
A vector containing the split string.
305 {
306  std::vector<std::string> elements;
307  split(s, delimiter, std::back_inserter(elements));
308  return elements;
309 }
std::vector< std::string > split(const std::string &s, const char delimiter)
split Splits a given string into a vector of tokens using the given delimiter.
Definition: util.cpp:304

Here is the caller graph for this function:

bool geometrize::util::stringBeginsWith ( const std::string &  str,
const std::string &  prefix 
)

stringBeginsWith Checks if a string begins with the given prefix.

Parameters
strThe string to check.
prefixThe prefix to check for.
Returns
True if the string begins with the given prefix, false otherwise.
247 {
248  return str.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), str.begin());
249 }

Here is the caller graph for this function:

bool geometrize::util::stringEndsWith ( const std::string &  str,
const std::string &  suffix 
)

stringEndsWith Checks if a string ends with the given suffix.

Parameters
strThe string to check.
suffixThe suffix ending to check for.
Returns
True if the string ends with the given suffix, false otherwise.
252 {
253  return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
254 }

Here is the caller graph for this function:

bool geometrize::util::writeStringToFile ( const std::string &  str,
const std::string &  path 
)

writeStringToFile Writes the string to the file path, attempting to overwrite any existing file at the path.

Parameters
strThe string to write to file.
pathThe file path to write to.
Returns
True if the string was successfully written to the file, else false.
272 {
273  std::ofstream out(path, std::ios::trunc | std::ios::out);
274  if(!out.is_open()) {
275  return false;
276  }
277 
278  if(out) {
279  out << str;
280  }
281 
282  out.close();
283 
284  if(!out) {
285  return false;
286  }
287 
288  return true;
289 }
ModulePtr trunc(ModulePtr m=std::make_shared< Module >())
Definition: chaiscriptmathextras.h:320

Here is the call graph for this function:

Here is the caller graph for this function:

void geometrize::util::writeStringVector ( const std::vector< std::string > &  vec,
const std::string &  filePath 
)

writeStringVector Writes the string vector to the given filepath as JSON. Will attempt to overwrite any existing file.

Parameters
textThe vector of strings to write.
filePathThe path of the file to write to.
38 {
39  if(!geometrize::util::stringEndsWith(filePath, ".json")) {
40  assert(0 && "Failed to write string vector, check file extension");
41  }
42 
43  // Create folders up to the path if necessary
44  const QFileInfo fileInfo(QString::fromStdString(filePath));
45  if(!fileInfo.exists()) {
46  const bool createdDir{QDir().mkpath(fileInfo.absolutePath())};
47  if(!createdDir) {
48  assert(0 && "Failed to create directory for string vector");
49  return;
50  }
51  }
52 
53  std::ofstream file(filePath);
54  cereal::JSONOutputArchive archive(file);
55  archive(cereal::make_nvp("stringVector", vec));
56 }
bool stringEndsWith(const std::string &str, const std::string &suffix)
stringEndsWith Checks if a string ends with the given suffix.
Definition: util.cpp:251

Here is the call graph for this function:

Here is the caller graph for this function: