Geometrize  1.0
An application for geometrizing images into geometric primitives
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 script
14 {
15 
16 class Command;
17 class CommandHandler;
18 
19 }
20 
21 }
22 
23 namespace geometrize
24 {
25 
26 namespace util
27 {
28 
33 void printToConsole(const std::string& str);
34 
39 void printToAllScriptConsoleWidgets(const std::string& str);
40 
45 void messageBox(const std::string& str);
46 
50 void debugBreak();
51 
56 
62 bool fileExists(const std::string& filePath);
63 
69 bool directoryExists(const std::string& dirPath);
70 
76 bool directoriesExist(const std::vector<std::string>& dirPaths);
77 
82 bool directoryContainsFile(const std::string& dirPath, const std::string& fileName);
83 
88 bool createDirectory(const std::string& dirPath);
89 
95 std::string readFileAsString(const std::string& filePath);
96 
102 std::vector<std::string> getFilePathsForDirectory(const std::string& dirPath);
103 
109 std::vector<std::string> getSubdirectoriesForDirectory(const std::string& dirPath);
110 
117 std::vector<std::string> getFilesWithExtension(const std::string& dirPath, const std::string& extension);
118 
125 std::string getFirstFileWithExtension(const std::string& dirPath, const std::string& extension);
126 
133 std::string getFirstFileWithExtensions(const std::string& dirPath, const std::vector<std::string>& extensions);
134 
140 std::vector<std::string> getScriptsForPath(const std::string& path);
141 
147 std::vector<std::string> getScriptsForPaths(const std::vector<std::string>& dirPaths);
148 
154 std::string getDirectoryForFilePath(const std::string& filePath);
155 
161 std::string getFileNameForFilePath(const std::string& filePath);
162 
168 bool openInDefaultApplication(const std::string& path);
169 
175 bool revealInDefaultApplication(const std::string& path);
176 
180 void clearGlobalClipboard();
181 
186 std::string getGlobalClipboardText();
187 
192 void setGlobalClipboardText(const std::string& text);
193 
200 bool stringBeginsWith(const std::string& str, const std::string& prefix);
201 
208 bool stringEndsWith(const std::string& str, const std::string& suffix);
209 
214 std::string getApplicationDirectoryLocation();
215 
220 std::string getAppDataLocation();
221 
226 std::string getHomeDirectoryLocation();
227 
232 std::string getDesktopDirectoryLocation();
233 
240 bool writeStringToFile(const std::string& str, const std::string& path);
241 
247 std::string percentEncode(const std::string& str);
248 
256 int randomInRange(int lower, int upper);
257 
265 template<typename T>
266 T clamp(T value, T lower, T upper)
267 {
268  assert(lower <= upper);
269  if (value < lower) {
270  value = lower;
271  }
272  if (value > upper) {
273  value = upper;
274  }
275  return value;
276 }
277 
278 template<typename T>
279 void split(const std::string& s, const char delimiter, T result)
280 {
281  std::stringstream ss;
282  ss.str(s);
283  std::string item;
284  while (std::getline(ss, item, delimiter)) {
285  *(result++) = item;
286  }
287 }
288 
295 std::vector<std::string> split(const std::string& s, char delimiter);
296 
301 int getCursorX();
302 
307 int getCursorY();
308 
314 void setCursorPos(int x, int y);
315 
320 std::string getOperatingSystemProductType();
321 
328 bool saveWidgetScreenshot(const std::string& path, QWidget* widget);
329 
334 std::vector<std::string> getAllNamedWidgets();
335 
340 std::vector<std::string> getAllNamedGeometrizeWidgets();
341 
347 QWidget* getWidgetByName(const std::string& widgetName);
348 
353 std::vector<std::string> getAllNamedCommandHandlers();
354 
361 
367 void sendCommand(const std::string& target, const std::string& command);
368 
374 void sendCommand(const std::string& target, geometrize::script::Command& command);
375 
380 void broadcastCommand(const std::string& command);
381 
387 
393 std::string getFormattedTimestamp(const std::string& formatString);
394 
399 std::string getFilenameTimestamp();
400 
405 std::string getUuidString();
406 
413 void setWidgetSize(QWidget* widget, int width, int height);
414 
421 void setWidgetPosition(QWidget* widget, int x, int y);
422 
433 void arrangeWidgetsInGrid(const std::vector<QWidget*>& widgets, int centerX, int centerY, int xDim, int yDim, int resizeX, int resizeY);
434 
444 void arrangeImageTaskWidgetsInGrid(int centerX, int centerY, int xDim, int yDim, int resizeX, int resizeY);
445 
453 void arrangeWidgetsInGridOnMonitor(const std::vector<QWidget*>& widgets, int screenIdx, int resizeX, int resizeY);
454 
461 void arrangeImageTaskWidgetsInGridOnMonitor(int screenIdx, int resizeX, int resizeY);
462 
469 void arrangeWidgetsInGridOnPrimaryMonitor(const std::vector<QWidget*>& widgets, int resizeX, int resizeY);
470 
476 void arrangeImageTaskWidgetsInGridOnPrimaryMonitor(int resizeX, int resizeY);
477 
483 void fitWidgetsInGridOnMonitor(const std::vector<QWidget*>& widgets, int screenIdx);
484 
489 void fitImageTaskWidgetsInGridOnMonitor(int screenIdx);
490 
495 void fitWidgetsInGridOnPrimaryMonitor(const std::vector<QWidget*>& widgets);
496 
501 
502 }
503 
504 }
chaiscript::extras::math::trunc
ModulePtr trunc(ModulePtr m=std::make_shared< Module >())
Definition: chaiscriptmathextras.h:320
geometrize::util::arrangeImageTaskWidgetsInGrid
void arrangeImageTaskWidgetsInGrid(int centerX, int centerY, int xDim, int yDim, int resizeX, int resizeY)
arrangeImageTaskWidgetsInGrid Arranges all the open image task widgets into a grid shape
Definition: util.cpp:540
geometrize::util::printToConsole
void printToConsole(const std::string &str)
printToConsole A convenience function for printing a string to the command-line console.
Definition: util.cpp:87
geometrize::util::getFileNameForFilePath
std::string getFileNameForFilePath(const std::string &filePath)
getFileNameForFilePath Gets the filename from the given local file path.
Definition: util.cpp:273
geometrize::util::fitWidgetsInGridOnPrimaryMonitor
void fitWidgetsInGridOnPrimaryMonitor(const std::vector< QWidget * > &widgets)
fitWidgetsInGridOnPrimaryMonitor Fits the given widgets into a grid on the primary monitor
Definition: util.cpp:592
geometrize::util::debugBreak
void debugBreak()
debugBreak Forces the debugger to break/halt the application when this is called.
Definition: util.cpp:75
geometrize::util::getHomeDirectoryLocation
std::string getHomeDirectoryLocation()
getHomeDirectoryLocation Returns a path to where the user's home directory is.
Definition: util.cpp:324
geometrize::util::getCursorY
int getCursorY()
getCursorY Gets the y-coordinate of the cursor in global screen coordinates.
Definition: util.cpp:379
geometrize::util::getFirstFileWithExtensions
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:224
geometrize::util::directoryContainsFile
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:135
commandhandler.h
geometrize::util::getScriptsForPath
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:236
geometrize::util::getScriptsForPaths
std::vector< std::string > getScriptsForPaths(const std::vector< std::string > &dirPaths)
getScriptsForPaths Gets the absolute file paths to the script files (*.chai) for the given directorie...
Definition: util.cpp:252
geometrize::util::getWidgetByName
QWidget * getWidgetByName(const std::string &widgetName)
getWidgetByName Returns the first found instance of the existing widget with the given name
Definition: util.cpp:423
geometrize::dialog::ImageTaskWindow::getExistingImageTaskWindows
static std::vector< ImageTaskWindow * > getExistingImageTaskWindows()
getExistingImageTaskWindows Gets all of the existing image task windows.
Definition: imagetaskwindow.cpp:881
geometrize::util::clearGlobalClipboard
void clearGlobalClipboard()
clearGlobalClipboard Clears the global system clipboard contents.
Definition: util.cpp:289
geometrize::util::getFormattedTimestamp
std::string getFormattedTimestamp(const std::string &formatString)
getFormattedTimestamp Gets a formatted timestamp for the current local time
Definition: util.cpp:496
geometrize::util::getAllNamedWidgets
std::vector< std::string > getAllNamedWidgets()
getAllWidgetNames Returns a vector containing the names of all the existing widgets in the applicatio...
Definition: util.cpp:399
geometrize::util::fitWidgetsInGridOnMonitor
void fitWidgetsInGridOnMonitor(const std::vector< QWidget * > &widgets, int screenIdx)
fitWidgetsInGridOnMonitor Fits the given widgets into a grid on the given monitor
Definition: util.cpp:572
geometrize::util::arrangeImageTaskWidgetsInGridOnPrimaryMonitor
void arrangeImageTaskWidgetsInGridOnPrimaryMonitor(int resizeX, int resizeY)
arrangeImageTaskWidgetsInGridOnPrimaryMonitor Arranges all the open image task widgets into a grid on...
Definition: util.cpp:567
geometrize::util::readFileAsString
std::string readFileAsString(const std::string &filePath)
readFileAsString Reads a file, returning a string containing the contents.
Definition: util.cpp:155
geometrize::script::Command
The Command class provides a convenient way for a script to tell an object about something For exampl...
Definition: command.h:17
geometrize::util::revealInDefaultApplication
bool revealInDefaultApplication(const std::string &path)
revealInDefaultApplication Reveals the given path in the parent folder (or path with the filename tri...
Definition: util.cpp:283
geometrize::util::processApplicationEvents
void processApplicationEvents()
processApplicationEvents Processes all pending application events for the calling thread.
Definition: util.cpp:110
geometrize::util::getGlobalClipboardText
std::string getGlobalClipboardText()
getGlobalClipboardText Gets the global system clipboard text.
Definition: util.cpp:294
geometrize::util::getFilenameTimestamp
std::string getFilenameTimestamp()
getFilenameTimestamp Gets a filename timestamp string for the current local time
Definition: util.cpp:501
geometrize::util::getDirectoryForFilePath
std::string getDirectoryForFilePath(const std::string &filePath)
getDirectoryForFilePath Gets the directory path from the given local file path.
Definition: util.cpp:262
geometrize::script::CommandHandler
The CommandHandler class provides an interface that all objects that can handle/process script comman...
Definition: commandhandler.h:19
geometrize::util::directoriesExist
bool directoriesExist(const std::vector< std::string > &dirPaths)
directoriesExist Checks if the given directories exist, returns true if all do.
Definition: util.cpp:130
geometrize::util::getFilesWithExtension
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:203
geometrize::util::printToAllScriptConsoleWidgets
void printToAllScriptConsoleWidgets(const std::string &str)
printToAllScriptConsoleWidgets A convenience function for printing a string to all the GUI console wi...
Definition: util.cpp:92
geometrize::util::setCursorPos
void setCursorPos(const int x, const int y)
setCursorPos Sets the cursor position in global screen coordinates.
Definition: util.cpp:384
command.h
geometrize::util::getApplicationDirectoryLocation
std::string getApplicationDirectoryLocation()
getApplicationDirectory Get directory that the application is in.
Definition: util.cpp:314
geometrize::util::getOperatingSystemProductType
std::string getOperatingSystemProductType()
getOperatingSystemProductType Returns the product name of the operating system this application is ru...
Definition: util.cpp:389
geometrize::util::getCursorX
int getCursorX()
getCursorX Gets the x-coordinate of the cursor in global screen coordinates.
Definition: util.cpp:374
geometrize::util::writeStringToFile
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:334
geometrize::util::getAllNamedGeometrizeWidgets
std::vector< std::string > getAllNamedGeometrizeWidgets()
getAllWidgetNames Returns a vector containing the names of all the widgets in the application with "g...
Definition: util.cpp:411
geometrize::util::arrangeImageTaskWidgetsInGridOnMonitor
void arrangeImageTaskWidgetsInGridOnMonitor(int screenIdx, int resizeX, int resizeY)
arrangeImageTaskWidgetsInGridOnMonitor Arranges all the open image task widgets into a grid on the gi...
Definition: util.cpp:557
geometrize::util::sendCommand
void sendCommand(const std::string &target, const std::string &command)
sendCommand Sends a command to the given command handler
Definition: util.cpp:456
geometrize::util::createDirectory
bool createDirectory(const std::string &dirPath)
createDirectory Creates the directory at the given path, recursively creating subdirectories if neede...
Definition: util.cpp:150
geometrize
Definition: commandlineparser.cpp:73
geometrize::util::getDesktopDirectoryLocation
std::string getDesktopDirectoryLocation()
getDesktopDirectoryLocation Returns a path to where the user's desktop is.
Definition: util.cpp:329
geometrize::util::getFirstFileWithExtension
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:214
geometrize::util::saveWidgetScreenshot
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:394
geometrize::util::messageBox
void messageBox(const std::string &str)
messageBox A convenience function for displaying a message box containing a message.
Definition: util.cpp:103
geometrize::util::randomInRange
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:361
geometrize::util::arrangeWidgetsInGrid
void arrangeWidgetsInGrid(const std::vector< QWidget * > &widgets, int centerX, int centerY, int xDim, int yDim, int resizeX, int resizeY)
arrangeWidgetsInGrid Arranges the given widgets into a grid shape
Definition: util.cpp:521
scriptconsole.h
geometrize::util::getFilePathsForDirectory
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:166
geometrize::util::percentEncode
std::string percentEncode(const std::string &str)
percentEncode Returns a percent-encoded copy of the input string.
Definition: util.cpp:354
geometrize::util::setWidgetPosition
void setWidgetPosition(QWidget *widget, int x, int y)
setWidgetPosition Sets the widget to the given position.
Definition: util.cpp:516
geometrize::util::endsWith
bool endsWith(const std::string &value, const std::string &ending)
Definition: util.cpp:194
geometrize::util::stringBeginsWith
bool stringBeginsWith(const std::string &str, const std::string &prefix)
stringBeginsWith Checks if a string begins with the given prefix.
Definition: util.cpp:304
geometrize::util::setGlobalClipboardText
void setGlobalClipboardText(const std::string &text)
setClipboardText Sets the global system clipboard text.
Definition: util.cpp:299
geometrize::util::directoryExists
bool directoryExists(const std::string &dirPath)
directoryExists Checks if a directory exists, returns true if it does.
Definition: util.cpp:125
geometrize::util::broadcastCommand
void broadcastCommand(const std::string &command)
broadcastCommand Sends a command to all command handlers
Definition: util.cpp:480
geometrize::dialog::ScriptConsole::appendString
void appendString(const std::string &str)
appendString Appends a string to the console output window
Definition: scriptconsole.cpp:137
geometrize::util::getCommandHandlerByName
geometrize::script::CommandHandler * getCommandHandlerByName(const std::string &name)
getCommandHandlerByName Returns the first found instance of the existing command handler with the giv...
Definition: util.cpp:444
geometrize::script::CommandHandler::allCommandHandlers
static std::set< CommandHandler * > allCommandHandlers
Definition: commandhandler.h:28
geometrize::util::arrangeWidgetsInGridOnMonitor
void arrangeWidgetsInGridOnMonitor(const std::vector< QWidget * > &widgets, int screenIdx, int resizeX, int resizeY)
arrangeWidgetsInGridOnMonitor Arranges the given widgets into a grid on the given monitor
Definition: util.cpp:545
geometrize::util::clamp
T clamp(T value, T lower, T upper)
clamp Clamps a value within a range.
Definition: util.h:266
geometrize::util::getUuidString
std::string getUuidString()
getUuidString Gets a Universally Unique Identifier (UUID) as a string
Definition: util.cpp:506
geometrize::util::openInDefaultApplication
bool openInDefaultApplication(const std::string &path)
openInDefaultApplication Opens the given path in the default application.
Definition: util.cpp:278
geometrize::util::fileExists
bool fileExists(const std::string &filePath)
fileExists Checks if a file exists, returns true if it does.
Definition: util.cpp:120
geometrize::dialog::ScriptConsole
The ScriptConsole class models a command line and output window.
Definition: scriptconsole.h:26
geometrize::util::getAppDataLocation
std::string getAppDataLocation()
getAppDataLocation Returns a directory location where persistent application data can be stored.
Definition: util.cpp:319
geometrize::util::getSubdirectoriesForDirectory
std::vector< std::string > getSubdirectoriesForDirectory(const std::string &dirPath)
getSubdirectoriesForDirectory Gets the absolute paths for direct subdirectories of the given director...
Definition: util.cpp:180
geometrize::util::getAllNamedCommandHandlers
std::vector< std::string > getAllNamedCommandHandlers()
getAllNamedCommandHandlers Gets all of the existing objects in the application that implement the Com...
Definition: util.cpp:434
util.h
geometrize::util::arrangeWidgetsInGridOnPrimaryMonitor
void arrangeWidgetsInGridOnPrimaryMonitor(const std::vector< QWidget * > &widgets, int resizeX, int resizeY)
arrangeWidgetsInGridOnPrimaryMonitor Arranges the given widgets into a grid on the primary monitor
Definition: util.cpp:562
imagetaskwindow.h
geometrize::util::fitImageTaskWidgetsInGridOnPrimaryMonitor
void fitImageTaskWidgetsInGridOnPrimaryMonitor()
fitImageTaskWidgetsInGridOnPrimaryMonitor Fits all of the open image task widgets into a grid on the ...
Definition: util.cpp:597
geometrize::util::setWidgetSize
void setWidgetSize(QWidget *widget, int width, int height)
setWidgetSize Sets the size of the given widget in pixels.
Definition: util.cpp:511
geometrize::util::split
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:367
geometrize::util::fitImageTaskWidgetsInGridOnMonitor
void fitImageTaskWidgetsInGridOnMonitor(int screenIdx)
fitImageTaskWidgetsInGridOnMonitor Fits all of the open image task widgets into a grid on the given m...
Definition: util.cpp:587
geometrize::util::stringEndsWith
bool stringEndsWith(const std::string &str, const std::string &suffix)
stringEndsWith Checks if a string ends with the given suffix.
Definition: util.cpp:309