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 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.

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

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.

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

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.
97 {
98  const QString qFileName{QString::fromStdString(fileName)};
99  QDirIterator it(QString::fromStdString(dirPath));
100  while(it.hasNext()) {
101  it.next();
102  if(QFileInfo(it.filePath()).isFile()) {
103  if(it.fileName() == qFileName) {
104  return true;
105  }
106  }
107  }
108  return false;
109 }

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.
92 {
93  return QDir(QString::fromStdString(dirPath)).exists();
94 }

Here is the caller graph for this function:

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

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.
87 {
88  return QFile(QString::fromStdString(filePath)).exists();
89 }

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.
261 {
262  return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation).toStdString();
263 }

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.
256 {
257  return QCoreApplication::applicationDirPath().toStdString();
258 }

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.
311 {
312  return QCursor::pos().x();
313 }

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.
316 {
317  return QCursor::pos().y();
318 }

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.
209 {
210  const QFileInfo fileInfo{QString::fromStdString(filePath)};
211 
212  if(!fileInfo.exists()) {
213  assert(0 && "File does not exist");
214  }
215 
216  return fileInfo.absoluteDir().absolutePath().toStdString();
217 }
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.
123 {
124  std::vector<std::string> files;
125 
126  QDirIterator it(QString::fromStdString(dirPath));
127  while(it.hasNext()) {
128  it.next();
129  if(QFileInfo(it.filePath()).isFile()) {
130  files.push_back(it.filePath().toStdString());
131  }
132  }
133  return files;
134 }

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.
160 {
161  std::vector<std::string> files{getFilePathsForDirectory(dirPath)};
162 
163  files.erase(std::remove_if(files.begin(), files.end(), [&extension](const std::string& s) {
164  return !endsWith(s, extension);
165  }), files.end());
166 
167  return files;
168 }
bool endsWith(const std::string &value, const std::string &ending)
Definition: util.cpp:150
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:122

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.
171 {
172  const std::vector<std::string> files{getFilesWithExtension(dirPath, extension)};
173 
174  if(files.empty()) {
175  return "";
176  }
177  return files.front();
178 }
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:159

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.
181 {
182  for(const std::string& ext : extensions) {
183  const std::string s{getFirstFileWithExtension(dirPath, ext)};
184  if(!s.empty()) {
185  return s;
186  }
187  }
188 
189  return "";
190 }
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:170

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.
236 {
237  return QApplication::clipboard()->text().toStdString();
238 }

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.
266 {
267  return QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString();
268 }

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
326 {
327  return QSysInfo::productType().toStdString();
328 }

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.
193 {
194  std::vector<std::string> scripts;
195  if(!util::directoryExists(dirPath)) {
196  assert(0 && "Script directory does not exist");
197  return scripts;
198  }
199 
200  QDirIterator it(QString::fromStdString(dirPath), { "*.chai" }, QDir::Files, QDirIterator::Subdirectories);
201  while (it.hasNext()) {
202  scripts.push_back(it.next().toStdString());
203  }
204 
205  return scripts;
206 }
bool directoryExists(const std::string &dirPath)
directoryExists Checks if a directory exists, returns true if it does.
Definition: util.cpp:91

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.
137 {
138  std::vector<std::string> files;
139 
140  QDirIterator it(QString::fromStdString(dirPath));
141  while(it.hasNext()) {
142  it.next();
143  if(QFileInfo(it.filePath()).isDir()) {
144  files.push_back(it.filePath().toStdString());
145  }
146  }
147  return files;
148 }

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:180

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.
70 {
71  QMessageBox msgBox;
72  msgBox.setText(QString::fromStdString(str));
73  msgBox.exec();
74 }

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.
220 {
221  return QDesktopServices::openUrl(QUrl::fromUserInput(QString::fromStdString(path)));
222 }

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:180
std::string readFileAsString(const std::string &filePath)
readFileAsString Reads a file, returning a string containing the contents.
Definition: util.cpp:111
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:192
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:96
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.
291 {
292  const QString s{QString::fromStdString(str)};
293  const QString percentEncoded{QUrl::toPercentEncoding(s)};
294  return percentEncoded.toStdString();
295 }

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.
59 {
60  const auto& allWidgets = QApplication::topLevelWidgets();
61  for (const auto& widget : allWidgets) {
62  const auto& consoles = widget->findChildren<geometrize::dialog::ScriptConsole*>();
63  for(auto& console : consoles) {
64  console->appendString(str);
65  }
66  }
67 }
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.
54 {
55  QTextStream(stdout) << QString::fromStdString(str);
56 }

Here is the caller graph for this function:

void geometrize::util::processApplicationEvents ( )

processApplicationEvents Processes all pending application events for the calling thread.

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

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.
298 {
299  assert(lower <= upper);
300  return geometrize::commonutil::randomRange(lower, upper);
301 }

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.
112 {
113  QFile file(QString::fromStdString(filePath));
114 
115  if(file.exists() && file.open(QIODevice::ReadOnly | QIODevice::Text)) {
116  return QString(file.readAll()).toStdString();
117  }
118 
119  return "";
120 }

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:250

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.
225 {
226  const QUrl directoryPath{QUrl::fromUserInput(QString::fromStdString(path)).adjusted(QUrl::RemoveFilename)};
227  return QDesktopServices::openUrl(directoryPath);
228 }

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.
331 {
332  return widget->grab().save(QString::fromStdString(path), "png");
333 }

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.
321 {
322  QCursor::setPos(x, y);
323 }

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.
241 {
242  QApplication::clipboard()->setText(QString::fromStdString(text));
243 }

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.
304 {
305  std::vector<std::string> elements;
306  split(s, delimiter, std::back_inserter(elements));
307  return elements;
308 }
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:303

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.
246 {
247  return str.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), str.begin());
248 }

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.
251 {
252  return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
253 }

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.
271 {
272  std::ofstream out(path, std::ios::trunc | std::ios::out);
273  if(!out.is_open()) {
274  return false;
275  }
276 
277  if(out) {
278  out << str;
279  }
280 
281  out.close();
282 
283  if(!out) {
284  return false;
285  }
286 
287  return true;
288 }
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:250

Here is the call graph for this function:

Here is the caller graph for this function: