Sat, 29 Aug 2015 18:45:48 +0300
Refactor LDrawPathDialog and LDPaths
CMakeLists.txt | file | annotate | diff | comparison | revisions | |
src/actions.cpp | file | annotate | diff | comparison | revisions | |
src/dialogs.cpp | file | annotate | diff | comparison | revisions | |
src/dialogs.h | file | annotate | diff | comparison | revisions | |
src/dialogs/ldrawpathdialog.cpp | file | annotate | diff | comparison | revisions | |
src/dialogs/ldrawpathdialog.h | file | annotate | diff | comparison | revisions | |
src/dialogs/ldrawpathdialog.ui | file | annotate | diff | comparison | revisions | |
src/ldDocument.cpp | file | annotate | diff | comparison | revisions | |
src/ldDocument.h | file | annotate | diff | comparison | revisions | |
src/ldpaths.cpp | file | annotate | diff | comparison | revisions | |
src/ldpaths.h | file | annotate | diff | comparison | revisions | |
src/main.cpp | file | annotate | diff | comparison | revisions | |
src/primitives.cpp | file | annotate | diff | comparison | revisions | |
ui/ldrawpath.ui | file | annotate | diff | comparison | revisions |
--- a/CMakeLists.txt Sat Aug 29 17:07:39 2015 +0300 +++ b/CMakeLists.txt Sat Aug 29 18:45:48 2015 +0300 @@ -50,6 +50,7 @@ src/ldDocument.cpp src/ldObject.cpp src/ldObjectMath.cpp + src/ldpaths.cpp src/main.cpp src/mainWindow.cpp src/messageLog.cpp @@ -60,6 +61,7 @@ src/ringFinder.cpp src/version.cpp src/dialogs/colorselector.cpp + src/dialogs/ldrawpathdialog.cpp src/editmodes/abstractEditMode.cpp src/editmodes/circleMode.cpp src/editmodes/drawMode.cpp @@ -94,7 +96,9 @@ src/mainWindow.h src/editHistory.h src/format.h + src/ldpaths.h src/dialogs/colorselector.h + src/dialogs/ldrawpathdialog.h src/editmodes/abstractEditMode.h src/editmodes/circleMode.h src/editmodes/drawMode.h @@ -118,7 +122,6 @@ ui/intersector.ui ui/isecalc.ui ui/ldforge.ui - ui/ldrawpath.ui ui/makeprim.ui ui/newpart.ui ui/openprogress.ui @@ -128,6 +131,7 @@ ui/rotpoint.ui ui/ytruder.ui src/dialogs/colorselector.ui + src/dialogs/ldrawpathdialog.ui ) add_custom_target (codegeneration ALL
--- a/src/actions.cpp Sat Aug 29 17:07:39 2015 +0300 +++ b/src/actions.cpp Sat Aug 29 18:45:48 2015 +0300 @@ -37,6 +37,7 @@ #include "colors.h" #include "glCompiler.h" #include "ui_newpart.h" +#include "dialogs/ldrawpathdialog.h" #include "editmodes/abstractEditMode.h" EXTERN_CFGENTRY (Bool, DrawWireframe) @@ -50,6 +51,7 @@ EXTERN_CFGENTRY (Bool, DrawEdgeLines) EXTERN_CFGENTRY (Bool, DrawConditionalLines) EXTERN_CFGENTRY (Bool, DrawAxes) +EXTERN_CFGENTRY (String, LDrawPath) // ============================================================================= // @@ -165,7 +167,7 @@ // void MainWindow::slot_actionSetLDrawPath() { - (new LDrawPathDialog (true))->exec(); + (new LDrawPathDialog (cfg::LDrawPath, true))->exec(); } // =============================================================================
--- a/src/dialogs.cpp Sat Aug 29 17:07:39 2015 +0300 +++ b/src/dialogs.cpp Sat Aug 29 18:45:48 2015 +0300 @@ -38,7 +38,6 @@ #include "ldDocument.h" #include "dialogs.h" #include "ui_overlay.h" -#include "ui_ldrawpath.h" #include "ui_openprogress.h" #include "ui_extprogpath.h" #include "ui_about.h" @@ -160,107 +159,6 @@ // ============================================================================= // ============================================================================= -LDrawPathDialog::LDrawPathDialog (const bool validDefault, QWidget* parent, Qt::WindowFlags f) : - QDialog (parent, f), - m_validDefault (validDefault) -{ - ui = new Ui_LDPathUI; - ui->setupUi (this); - ui->status->setText ("---"); - - if (validDefault) - ui->heading->hide(); - else - { - cancelButton()->setText ("Exit"); - cancelButton()->setIcon (GetIcon ("exit")); - } - - okButton()->setEnabled (false); - - connect (ui->path, SIGNAL (textEdited (QString)), this, SLOT (slot_tryConfigure())); - connect (ui->searchButton, SIGNAL (clicked()), this, SLOT (slot_findPath())); - connect (ui->buttonBox, SIGNAL (rejected()), this, validDefault ? SLOT (reject()) : SLOT (slot_exit())); - connect (ui->buttonBox, SIGNAL (accepted()), this, SLOT (slot_accept())); - - setPath (cfg::LDrawPath); - - if (validDefault) - slot_tryConfigure(); -} - -// ============================================================================= -// ============================================================================= -LDrawPathDialog::~LDrawPathDialog() -{ - delete ui; -} - -QPushButton* LDrawPathDialog::okButton() -{ - return ui->buttonBox->button (QDialogButtonBox::Ok); -} - -QPushButton* LDrawPathDialog::cancelButton() -{ - return ui->buttonBox->button (QDialogButtonBox::Cancel); -} - -void LDrawPathDialog::setPath (QString path) -{ - ui->path->setText (path); -} - -QString LDrawPathDialog::filename() const -{ - return ui->path->text(); -} - -// ============================================================================= -// ============================================================================= -void LDrawPathDialog::slot_findPath() -{ - QString newpath = QFileDialog::getExistingDirectory (this, "Find LDraw Path"); - - if (not newpath.isEmpty()) - { - setPath (newpath); - slot_tryConfigure(); - } -} - -// ============================================================================= -// ============================================================================= -void LDrawPathDialog::slot_exit() -{ - Exit(); -} - -// ============================================================================= -// ============================================================================= -void LDrawPathDialog::slot_tryConfigure() -{ - if (not LDPaths::tryConfigure (filename())) - { - ui->status->setText (format ("<span style=\"color:#700; \">%1</span>", LDPaths::getError())); - okButton()->setEnabled (false); - return; - } - - ui->status->setText ("<span style=\"color: #270; \">OK!</span>"); - okButton()->setEnabled (true); -} - -// ============================================================================= -// ============================================================================= -void LDrawPathDialog::slot_accept() -{ - Config::Save(); - accept(); -} - -// ============================================================================= -// ============================================================================= OpenProgressDialog::OpenProgressDialog (QWidget* parent, Qt::WindowFlags f) : QDialog (parent, f) { ui = new Ui_OpenProgressUI;
--- a/src/dialogs.h Sat Aug 29 17:07:39 2015 +0300 +++ b/src/dialogs.h Sat Aug 29 18:45:48 2015 +0300 @@ -65,31 +65,6 @@ }; // ============================================================================= -class LDrawPathDialog : public QDialog -{ - Q_OBJECT - -public: - explicit LDrawPathDialog (const bool validDefault, QWidget* parent = null, Qt::WindowFlags f = 0); - virtual ~LDrawPathDialog(); - QString filename() const; - void setPath (QString path); - -private: - Q_DISABLE_COPY (LDrawPathDialog) - const bool m_validDefault; - Ui_LDPathUI* ui; - QPushButton* okButton(); - QPushButton* cancelButton(); - -private slots: - void slot_findPath(); - void slot_tryConfigure(); - void slot_exit(); - void slot_accept(); -}; - -// ============================================================================= class OpenProgressDialog : public QDialog { Q_OBJECT
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/dialogs/ldrawpathdialog.cpp Sat Aug 29 18:45:48 2015 +0300 @@ -0,0 +1,89 @@ +#include <QFileDialog> +#include <QPushButton> +#include <QLabel> +#include "ldrawpathdialog.h" +#include "ui_ldrawpathdialog.h" +#include "../mainWindow.h" + +LDrawPathDialog::LDrawPathDialog (const QString& defaultPath, bool validDefault, QWidget* parent, Qt::WindowFlags f) : + QDialog (parent, f), + m_hasValidDefault (validDefault), + ui (*new Ui_LDrawPathDialog) +{ + ui.setupUi (this); + ui.status->setText ("---"); + + if (validDefault) + ui.heading->hide(); + else + { + cancelButton()->setText ("Exit"); + cancelButton()->setIcon (GetIcon ("exit")); + } + + okButton()->setEnabled (false); + + connect (ui.path, SIGNAL (textChanged (QString)), this, SIGNAL (pathChanged (QString))); + connect (ui.searchButton, SIGNAL (clicked()), this, SLOT (searchButtonClicked())); + connect (ui.buttonBox, SIGNAL (rejected()), this, validDefault ? SLOT (reject()) : SLOT (slot_exit())); + connect (ui.buttonBox, SIGNAL (accepted()), this, SLOT (slot_accept())); + setPath (defaultPath); +} + +LDrawPathDialog::~LDrawPathDialog() +{ + delete &ui; +} + +QPushButton* LDrawPathDialog::okButton() +{ + return ui.buttonBox->button (QDialogButtonBox::Ok); +} + +QPushButton* LDrawPathDialog::cancelButton() +{ + return ui.buttonBox->button (QDialogButtonBox::Cancel); +} + +void LDrawPathDialog::setPath (QString path) +{ + ui.path->setText (path); +} + +QString LDrawPathDialog::path() const +{ + return ui.path->text(); +} + +void LDrawPathDialog::searchButtonClicked() +{ + QString newpath = QFileDialog::getExistingDirectory (this, "Find LDraw Path"); + + if (not newpath.isEmpty()) + setPath (newpath); +} + +void LDrawPathDialog::slot_exit() +{ + Exit(); +} + +void LDrawPathDialog::setStatusText (const QString& statusText, bool ok) +{ + okButton()->setEnabled (ok); + + if (statusText.isEmpty() && ok == false) + ui.status->setText ("---"); + else + { + ui.status->setText (QString ("<span style=\"color: %1\">%2</span>") + .arg (ok ? "#700" : "#270") + .arg (statusText)); + } +} + +void LDrawPathDialog::slot_accept() +{ + Config::Save(); + accept(); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/dialogs/ldrawpathdialog.h Sat Aug 29 18:45:48 2015 +0300 @@ -0,0 +1,29 @@ +#pragma once +#include <QDialog> +#include "../main.h" + +class LDrawPathDialog : public QDialog +{ + Q_OBJECT + +public: + LDrawPathDialog (const QString& defaultPath, bool validDefault, QWidget* parent = null, Qt::WindowFlags f = 0); + virtual ~LDrawPathDialog(); + QString path() const; + void setPath (QString path); + void setStatusText (const QString& statusText, bool ok); + +signals: + void pathChanged (QString newPath); + +private: + const bool m_hasValidDefault; + class Ui_LDrawPathDialog& ui; + QPushButton* okButton(); + QPushButton* cancelButton(); + +private slots: + void searchButtonClicked(); + void slot_exit(); + void slot_accept(); +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/dialogs/ldrawpathdialog.ui Sat Aug 29 18:45:48 2015 +0300 @@ -0,0 +1,88 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>LDrawPathDialog</class> + <widget class="QDialog" name="LDrawPathDialog"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>344</width> + <height>140</height> + </rect> + </property> + <property name="windowTitle"> + <string>Set LDraw Path</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QLabel" name="heading"> + <property name="text"> + <string>Please input your LDraw directory root to proceed:</string> + </property> + </widget> + </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <widget class="QLabel" name="label"> + <property name="text"> + <string>LDraw Path:</string> + </property> + </widget> + </item> + <item> + <widget class="QLineEdit" name="path"/> + </item> + <item> + <widget class="QPushButton" name="searchButton"> + <property name="text"> + <string/> + </property> + <property name="icon"> + <iconset resource="../../ldforge.qrc"> + <normaloff>:/icons/folder.png</normaloff>:/icons/folder.png</iconset> + </property> + </widget> + </item> + </layout> + </item> + <item> + <widget class="QLabel" name="status"> + <property name="styleSheet"> + <string notr="true">font-weight: bold</string> + </property> + <property name="text"> + <string>[[ Information ]]</string> + </property> + </widget> + </item> + <item> + <spacer name="verticalSpacer"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>20</width> + <height>40</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QDialogButtonBox" name="buttonBox"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="standardButtons"> + <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> + </property> + </widget> + </item> + </layout> + </widget> + <resources> + <include location="../../ldforge.qrc"/> + </resources> + <connections/> +</ui>
--- a/src/ldDocument.cpp Sat Aug 29 17:07:39 2015 +0300 +++ b/src/ldDocument.cpp Sat Aug 29 18:45:48 2015 +0300 @@ -21,23 +21,23 @@ #include <QDir> #include <QTime> #include <QApplication> - #include "main.h" #include "configuration.h" #include "ldDocument.h" #include "miscallenous.h" #include "mainWindow.h" #include "editHistory.h" -#include "dialogs.h" #include "glRenderer.h" #include "glCompiler.h" #include "partDownloader.h" +#include "ldpaths.h" +#include "dialogs.h" -CFGENTRY (String, LDrawPath, "") CFGENTRY (List, RecentFiles, {}) CFGENTRY (Bool, TryDownloadMissingFiles, false) EXTERN_CFGENTRY (String, DownloadFilePath) EXTERN_CFGENTRY (Bool, UseLogoStuds) +EXTERN_CFGENTRY (String, LDrawPath) static bool g_loadingMainFile = false; static const int g_maxRecentFiles = 10; @@ -53,79 +53,6 @@ // ============================================================================= // -namespace LDPaths -{ - static QString pathError; - - struct - { - QString LDConfigPath; - QString partsPath, primsPath; - } pathInfo; - - void initPaths() - { - if (not tryConfigure (cfg::LDrawPath)) - { - LDrawPathDialog dlg (false); - - if (not dlg.exec()) - Exit(); - - cfg::LDrawPath = dlg.filename(); - } - } - - bool tryConfigure (QString path) - { - QDir dir; - - if (not dir.cd (path)) - { - pathError = "Directory does not exist."; - return false; - } - - QStringList mustHave = { "LDConfig.ldr", "parts", "p" }; - QStringList contents = dir.entryList (mustHave); - - if (contents.size() != mustHave.size()) - { - pathError = "Not an LDraw directory! Must<br />have LDConfig.ldr, parts/ and p/."; - return false; - } - - pathInfo.partsPath = format ("%1" DIRSLASH "parts", path); - pathInfo.LDConfigPath = format ("%1" DIRSLASH "LDConfig.ldr", path); - pathInfo.primsPath = format ("%1" DIRSLASH "p", path); - - return true; - } - - // Accessors - QString getError() - { - return pathError; - } - - QString ldconfig() - { - return pathInfo.LDConfigPath; - } - - QString prims() - { - return pathInfo.primsPath; - } - - QString parts() - { - return pathInfo.partsPath; - } -} - -// ============================================================================= -// LDDocument::LDDocument() : m_isImplicit (true), m_flags (0),
--- a/src/ldDocument.h Sat Aug 29 17:07:39 2015 +0300 +++ b/src/ldDocument.h Sat Aug 29 18:45:48 2015 +0300 @@ -28,17 +28,6 @@ struct LDGLData; class GLCompiler; -namespace LDPaths -{ - void initPaths(); - bool tryConfigure (QString path); - - QString ldconfig(); - QString prims(); - QString parts(); - QString getError(); -} - // // Flags for LDDocument //
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ldpaths.cpp Sat Aug 29 18:45:48 2015 +0300 @@ -0,0 +1,85 @@ +#include <QDir> +#include "ldpaths.h" +#include "dialogs/ldrawpathdialog.h" + +CFGENTRY (String, LDrawPath, "") + +LDPaths::LDPaths (QObject* parent) : + QObject (parent), + m_dialog (nullptr) {} + +void LDPaths::checkPaths() +{ + if (not configurePaths (cfg::LDrawPath)) + { + m_dialog = new LDrawPathDialog (cfg::LDrawPath, false); + connect (m_dialog, SIGNAL (pathChanged(QString)), this, SLOT (configurePaths (QString))); + + if (not m_dialog->exec()) + Exit(); + else + cfg::LDrawPath = m_dialog->path(); + } +} + +bool LDPaths::isValid (const QDir& dir) const +{ + if (dir.exists() && dir.isReadable()) + { + QStringList mustHave = { "LDConfig.ldr", "parts", "p" }; + QStringList contents = dir.entryList (mustHave); + + if (contents.size() == mustHave.size()) + m_error = ""; + else + m_error = "Not an LDraw directory! Must<br />have LDConfig.ldr, parts/ and p/."; + } + else + m_error = "Directory does not exist or is not readable."; + + return m_error.isEmpty(); +} + +bool LDPaths::configurePaths (QString path) +{ + QDir dir; + dir.cd (path); + bool ok = isValid (dir); + + if (ok) + { + baseDir() = dir; + ldConfigPath() = format ("%1" DIRSLASH "LDConfig.ldr", path); + partsDir() = QDir (path + DIRSLASH "parts"); + primitivesDir() = QDir (path + DIRSLASH "p"); + } + + if (m_dialog) + m_dialog->setStatusText (m_error.isEmpty() ? "OK" : m_error, ok); + + return ok; +} + +QString& LDPaths::ldConfigPath() +{ + static QString value; + return value; +} + +QDir& LDPaths::primitivesDir() +{ + static QDir value; + return value; +} + +QDir& LDPaths::partsDir() +{ + static QDir value; + return value; +} + +QDir& LDPaths::baseDir() +{ + static QDir value; + return value; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ldpaths.h Sat Aug 29 18:45:48 2015 +0300 @@ -0,0 +1,26 @@ +#pragma once +#include "main.h" + +class QDir; + +class LDPaths : public QObject +{ + Q_OBJECT + +public: + LDPaths (QObject* parent = nullptr); + void checkPaths(); + bool isValid (const class QDir& path) const; + + static QDir& baseDir(); + static QString& ldConfigPath(); + static QDir& primitivesDir(); + static QDir& partsDir(); + +public slots: + bool configurePaths (QString path); + +private: + mutable QString m_error; + class LDrawPathDialog* m_dialog; +}; \ No newline at end of file
--- a/src/main.cpp Sat Aug 29 17:07:39 2015 +0300 +++ b/src/main.cpp Sat Aug 29 18:45:48 2015 +0300 @@ -33,6 +33,7 @@ #include "configDialog.h" #include "dialogs.h" #include "crashCatcher.h" +#include "ldpaths.h" MainWindow* g_win = null; static QString g_versionString, g_fullVersionString; @@ -64,7 +65,9 @@ Critical ("Failed to create configuration file!\n"); } - LDPaths::initPaths(); + LDPaths* paths = new LDPaths; + paths->checkPaths(); + paths->deleteLater(); InitColors(); LoadPrimitives(); MainWindow* win = new MainWindow;
--- a/src/primitives.cpp Sat Aug 29 17:07:39 2015 +0300 +++ b/src/primitives.cpp Sat Aug 29 18:45:48 2015 +0300 @@ -25,6 +25,7 @@ #include "ui_makeprim.h" #include "miscallenous.h" #include "colors.h" +#include "ldpaths.h" QList<PrimitiveCategory*> g_PrimitiveCategories; QList<Primitive> g_primitives; @@ -112,7 +113,7 @@ m_i (0) { g_activeScanner = this; - QDir dir (LDPaths::prims()); + QDir dir = LDPaths::primitivesDir(); assert (dir.exists()); m_baselen = dir.absolutePath().length(); GetRecursiveFilenames (dir, m_files);
--- a/ui/ldrawpath.ui Sat Aug 29 17:07:39 2015 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,88 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<ui version="4.0"> - <class>LDPathUI</class> - <widget class="QDialog" name="LDPathUI"> - <property name="geometry"> - <rect> - <x>0</x> - <y>0</y> - <width>344</width> - <height>123</height> - </rect> - </property> - <property name="windowTitle"> - <string>Set LDraw Path</string> - </property> - <layout class="QVBoxLayout" name="verticalLayout"> - <item> - <widget class="QLabel" name="heading"> - <property name="text"> - <string>Please input your LDraw directory root to proceed:</string> - </property> - </widget> - </item> - <item> - <layout class="QHBoxLayout" name="horizontalLayout"> - <item> - <widget class="QLabel" name="label"> - <property name="text"> - <string>LDraw Path:</string> - </property> - </widget> - </item> - <item> - <widget class="QLineEdit" name="path"/> - </item> - <item> - <widget class="QPushButton" name="searchButton"> - <property name="text"> - <string/> - </property> - <property name="icon"> - <iconset resource="../../ldforge.qrc"> - <normaloff>:/icons/folder.png</normaloff>:/icons/folder.png</iconset> - </property> - </widget> - </item> - </layout> - </item> - <item> - <widget class="QLabel" name="status"> - <property name="styleSheet"> - <string notr="true">font-weight: bold</string> - </property> - <property name="text"> - <string>[[ Information ]]</string> - </property> - </widget> - </item> - <item> - <spacer name="verticalSpacer"> - <property name="orientation"> - <enum>Qt::Vertical</enum> - </property> - <property name="sizeHint" stdset="0"> - <size> - <width>20</width> - <height>40</height> - </size> - </property> - </spacer> - </item> - <item> - <widget class="QDialogButtonBox" name="buttonBox"> - <property name="orientation"> - <enum>Qt::Horizontal</enum> - </property> - <property name="standardButtons"> - <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> - </property> - </widget> - </item> - </layout> - </widget> - <resources> - <include location="../../ldforge.qrc"/> - </resources> - <connections/> -</ui>