Geometrize  1.0
An application for geometrizing images into geometric primitives
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
util.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cassert>
4 #include <string>
5 #include <sstream>
6 #include <vector>
7 
8 class QWidget;
9 
10 namespace geometrize
11 {
12 
13 namespace util
14 {
15 
20 void printToConsole(const std::string& str);
21 
26 void printToAllScriptConsoleWidgets(const std::string& str);
27 
32 void messageBox(const std::string& str);
33 
37 void debugBreak();
38 
43 
49 bool fileExists(const std::string& filePath);
50 
56 bool directoryExists(const std::string& dirPath);
57 
62 bool directoryContainsFile(const std::string& dirPath, const std::string& fileName);
63 
69 std::string readFileAsString(const std::string& filePath);
70 
76 std::vector<std::string> getFilePathsForDirectory(const std::string& dirPath);
77 
83 std::vector<std::string> getSubdirectoriesForDirectory(const std::string& dirPath);
84 
91 std::vector<std::string> getFilesWithExtension(const std::string& dirPath, const std::string& extension);
92 
99 std::string getFirstFileWithExtension(const std::string& dirPath, const std::string& extension);
100 
107 std::string getFirstFileWithExtensions(const std::string& dirPath, const std::vector<std::string>& extensions);
108 
114 std::vector<std::string> getScriptsForPath(const std::string& path);
115 
121 std::string getDirectoryForFilePath(const std::string& filePath);
122 
128 bool openInDefaultApplication(const std::string& path);
129 
135 bool revealInDefaultApplication(const std::string& path);
136 
140 void clearGlobalClipboard();
141 
146 std::string getGlobalClipboardText();
147 
152 void setGlobalClipboardText(const std::string& text);
153 
160 bool stringBeginsWith(const std::string& str, const std::string& prefix);
161 
168 bool stringEndsWith(const std::string& str, const std::string& suffix);
169 
174 std::string getApplicationDirectoryLocation();
175 
180 std::string getAppDataLocation();
181 
186 std::string getHomeDirectoryLocation();
187 
194 bool writeStringToFile(const std::string& str, const std::string& path);
195 
201 std::string percentEncode(const std::string& str);
202 
210 int randomInRange(int lower, int upper);
211 
219 template<typename T>
220 T clamp(T value, T lower, T upper)
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 }
231 
232 template<typename T>
233 void split(const std::string& s, const char delimiter, T 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 }
242 
249 std::vector<std::string> split(const std::string& s, char delimiter);
250 
255 int getCursorX();
256 
261 int getCursorY();
262 
268 void setCursorPos(int x, int y);
269 
274 std::string getOperatingSystemProductType();
275 
281 bool saveDesktopScreenshot(const std::string& path);
282 
289 bool saveWidgetScreenshot(const std::string& path, QWidget* widget);
290 
291 }
292 
293 }
bool openInDefaultApplication(const std::string &path)
openInDefaultApplication Opens the given path in the default application.
Definition: util.cpp:220
std::string getAppDataLocation()
getAppDataLocation Returns a directory location where persistent application data can be stored...
Definition: util.cpp:261
int getCursorX()
getCursorX Gets the x-coordinate of the cursor in global screen coordinates.
Definition: util.cpp:311
bool fileExists(const std::string &filePath)
fileExists Checks if a file exists, returns true if it does.
Definition: util.cpp:87
void clearGlobalClipboard()
clearGlobalClipboard Clears the global system clipboard contents.
Definition: util.cpp:231
void printToAllScriptConsoleWidgets(const std::string &str)
printToAllScriptConsoleWidgets A convenience function for printing a string to all the GUI console wi...
Definition: util.cpp:59
std::string getGlobalClipboardText()
getGlobalClipboardText Gets the global system clipboard text.
Definition: util.cpp:236
void processApplicationEvents()
processApplicationEvents Processes all pending application events for the calling thread...
Definition: util.cpp:77
std::string getHomeDirectoryLocation()
getHomeDirectoryLocation Returns a path to where the user's home directory is.
Definition: util.cpp:266
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...
Definition: util.cpp:344
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
bool revealInDefaultApplication(const std::string &path)
revealInDefaultApplication Reveals the given path in the parent folder (or path with the filename tri...
Definition: util.cpp:225
bool stringEndsWith(const std::string &str, const std::string &suffix)
stringEndsWith Checks if a string ends with the given suffix.
Definition: util.cpp:251
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
void setCursorPos(const int x, const int y)
setCursorPos Sets the cursor position in global screen coordinates.
Definition: util.cpp:321
std::string percentEncode(const std::string &str)
percentEncode Returns a percent-encoded copy of the input string.
Definition: util.cpp:291
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
bool directoryExists(const std::string &dirPath)
directoryExists Checks if a directory exists, returns true if it does.
Definition: util.cpp:92
void printToConsole(const std::string &str)
printToConsole A convenience function for printing a string to the command-line console.
Definition: util.cpp:54
bool saveDesktopScreenshot(const std::string &path)
saveDesktopScreenshot Takes a screenshot of the desktop and saves it as a .png image to the given pat...
Definition: util.cpp:331
int getCursorY()
getCursorY Gets the y-coordinate of the cursor in global screen coordinates.
Definition: util.cpp:316
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
void messageBox(const std::string &str)
messageBox A convenience function for displaying a message box containing a message.
Definition: util.cpp:70
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 getApplicationDirectoryLocation()
getApplicationDirectory Get directory that the application is in.
Definition: util.cpp:256
bool stringBeginsWith(const std::string &str, const std::string &prefix)
stringBeginsWith Checks if a string begins with the given prefix.
Definition: util.cpp:246
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
std::string getDirectoryForFilePath(const std::string &filePath)
getDirectoryForFilePath Gets the directory path from the given local file path.
Definition: util.cpp:209
T clamp(T value, T lower, T upper)
clamp Clamps a value within a range.
Definition: util.h:220
std::vector< std::string > getSubdirectoriesForDirectory(const std::string &dirPath)
getSubdirectoriesForDirectory Gets the absolute paths for direct subdirectories of the given director...
Definition: util.cpp:137
void debugBreak()
debugBreak Forces the debugger to break/halt the application when this is called. ...
Definition: util.cpp:42
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
void setGlobalClipboardText(const std::string &text)
setClipboardText Sets the global system clipboard text.
Definition: util.cpp:241
std::string getOperatingSystemProductType()
getOperatingSystemProductType Returns the product name of the operating system this application is ru...
Definition: util.cpp:326
int randomInRange(const int lower, const int upper)
randomInRange Returns a random integer in the range [lower, upper], inclusive. Note that this uses th...
Definition: util.cpp:298
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 th...
Definition: util.cpp:271