Fri, 12 Apr 2013 00:28:17 +0300
Added user-configurable quick-coloring toolbar for.. quick coloring.
colors.cpp | file | annotate | diff | comparison | revisions | |
colors.h | file | annotate | diff | comparison | revisions | |
gui.cpp | file | annotate | diff | comparison | revisions | |
gui.h | file | annotate | diff | comparison | revisions | |
gui_actions.cpp | file | annotate | diff | comparison | revisions | |
main.cpp | file | annotate | diff | comparison | revisions | |
zz_configDialog.cpp | file | annotate | diff | comparison | revisions | |
zz_configDialog.h | file | annotate | diff | comparison | revisions | |
zz_historyDialog.cpp | file | annotate | diff | comparison | revisions |
--- a/colors.cpp Thu Apr 11 17:15:01 2013 +0300 +++ b/colors.cpp Fri Apr 12 00:28:17 2013 +0300 @@ -73,6 +73,20 @@ // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= +short color::index () { + short idx = 0; + for (color* it : g_LDColors) { + if (it == this) + return idx; + idx++; + } + + return -1; +} + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= void parseLDConfig () { FILE* fp = openLDrawFile ("LDConfig.ldr", false);
--- a/colors.h Thu Apr 11 17:15:01 2013 +0300 +++ b/colors.h Fri Apr 12 00:28:17 2013 +0300 @@ -28,6 +28,8 @@ public: str zName, zColorString; QColor qColor, qEdge; + + short index (); }; typedef struct {
--- a/gui.cpp Thu Apr 11 17:15:01 2013 +0300 +++ b/gui.cpp Fri Apr 12 00:28:17 2013 +0300 @@ -68,6 +68,7 @@ cfg (bool, lv_colorize, true); cfg (int, gui_toolbar_iconsize, 24); +cfg (str, gui_colortoolbar, "16:24:|:0:1:2:3:4:5:6:7"); extern_cfg (str, io_recentfiles); // ============================================================================= @@ -97,6 +98,8 @@ w->setLayout (layout); setCentralWidget (w); + quickColorMeta = parseQuickColorMeta (); + createMenuActions (); createMenus (); createToolbars (); @@ -269,6 +272,11 @@ ADD_TOOLBAR_ITEM (moveDown) // ========================================== + // Color toolbar + qColorToolBar = new QToolBar; + addToolBar (Qt::RightToolBarArea, qColorToolBar); + + // ========================================== // Left area toolbars g_ToolBarArea = Qt::LeftToolBarArea; initSingleToolBar ("Objects"); @@ -282,9 +290,55 @@ updateToolBars (); } +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +std::vector<quickColorMetaEntry> parseQuickColorMeta () { + std::vector<quickColorMetaEntry> meta; + + for (str zColor : gui_colortoolbar.value / ":") { + if (zColor == "|") { + meta.push_back ({nullptr, nullptr, true}); + } else { + color* col = getColor (atoi (zColor)); + meta.push_back ({col, nullptr, false}); + } + } + + return meta; +} + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= void ForgeWindow::updateToolBars () { - for (QToolBar* qBar : qaToolBars) { + for (QToolBar* qBar : qaToolBars) qBar->setIconSize (QSize (gui_toolbar_iconsize, gui_toolbar_iconsize)); + + // Update the quick color toolbar. + for (QPushButton* qButton : qaColorButtons) + delete qButton; + + qaColorButtons.clear (); + + // Clear the toolbar to remove separators + qColorToolBar->clear (); + + for (quickColorMetaEntry& entry : quickColorMeta) { + if (entry.bSeparator) + qColorToolBar->addSeparator (); + else { + QPushButton* qColorButton = new QPushButton; + qColorButton->setAutoFillBackground (true); + qColorButton->setStyleSheet (str::mkfmt ("background-color: %s", entry.col->zColorString.chars())); + qColorButton->setToolTip (entry.col->zName); + + connect (qColorButton, SIGNAL (clicked ()), this, SLOT (slot_quickColor ())); + qColorToolBar->addWidget (qColorButton); + qaColorButtons.push_back (qColorButton); + + entry.btn = qColorButton; + } } } @@ -512,6 +566,41 @@ // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= +void ForgeWindow::slot_quickColor () { + QPushButton* qBtn = static_cast<QPushButton*> (sender ()); + color* col = nullptr; + + for (quickColorMetaEntry entry : quickColorMeta) { + if (entry.btn == qBtn) { + col = entry.col; + break; + } + } + + if (col == nullptr) + return; + + std::vector<ulong> ulaIndices; + std::vector<short> daColors; + short dNewColor = col->index (); + + for (LDObject* obj : getSelectedObjects ()) { + if (obj->dColor == -1) + continue; // uncolored object + + ulaIndices.push_back (obj->getIndex (g_CurrentFile)); + daColors.push_back (obj->dColor); + + obj->dColor = dNewColor; + } + + History::addEntry (new SetColorHistory (ulaIndices, daColors, dNewColor)); + refresh (); +} + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= ulong ForgeWindow::getInsertionPoint () { ulong ulIndex; @@ -560,4 +649,10 @@ // ============================================================================= QIcon getIcon (const char* sIconName) { return (QIcon (str::mkfmt ("./icons/%s.png", sIconName))); +} + +// ============================================================================= +bool confirm (str zMessage) { + return QMessageBox::question (g_ForgeWindow, "Confirm", zMessage, + (QMessageBox::Yes | QMessageBox::No), QMessageBox::No) == QMessageBox::Yes; } \ No newline at end of file
--- a/gui.h Thu Apr 11 17:15:01 2013 +0300 +++ b/gui.h Fri Apr 12 00:28:17 2013 +0300 @@ -26,6 +26,7 @@ #include <QTreeWidget> #include <QToolBar> #include <QTextEdit> +#include <qpushbutton.h> #include "gldraw.h" #include "config.h" @@ -66,6 +67,13 @@ #define SHIFT(N) (Qt::SHIFT | Qt::Key_##N) #define CTRL_SHIFT(N) (Qt::CTRL | Qt::SHIFT | Qt::Key_##N) +class color; +typedef struct { + color* col; + QPushButton* btn; + bool bSeparator; +} quickColorMetaEntry; + // ============================================================================= // ActionAdder // @@ -110,6 +118,10 @@ std::vector<QToolBar*> qaToolBars; + std::vector<QPushButton*> qaColorButtons; + QToolBar* qColorToolBar; + std::vector<quickColorMetaEntry> quickColorMeta; + str zMessageLogHTML; ForgeWindow (); @@ -132,11 +144,14 @@ void slot_selectionChanged (); void slot_action (); void slot_recentFile (); + void slot_quickColor (); }; // ----------------------------------------------------------------------------- // Other GUI-related stuff not directly part of ForgeWindow: QIcon getIcon (const char* sIconName); +std::vector<quickColorMetaEntry> parseQuickColorMeta (); +bool confirm (str zMessage); // ----------------------------------------------------------------------------- // Pointer to the instance of ForgeWindow.
--- a/gui_actions.cpp Thu Apr 11 17:15:01 2013 +0300 +++ b/gui_actions.cpp Fri Apr 12 00:28:17 2013 +0300 @@ -83,7 +83,7 @@ // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= ACTION (settings, "Settin&gs", "settings", "Edit the settings of " APPNAME_DISPLAY ".", (0)) { - ConfigDialog::staticDialog (g_ForgeWindow); + ConfigDialog::staticDialog (); } // =============================================================================
--- a/main.cpp Thu Apr 11 17:15:01 2013 +0300 +++ b/main.cpp Fri Apr 12 00:28:17 2013 +0300 @@ -33,7 +33,7 @@ const vertex g_Origin (0.0f, 0.0f, 0.0f); -int main (int dArgC, char* saArgV[]) { +int main (int dArgc, char* saArgv[]) { // Load or create the configuration if (!config::load()) { printf ("Creating configuration file...\n"); @@ -45,7 +45,7 @@ initColors (); - QApplication app (dArgC, saArgV); + QApplication app (dArgc, saArgv); ForgeWindow* win = new ForgeWindow; g_qMainApp = &app;
--- a/zz_configDialog.cpp Thu Apr 11 17:15:01 2013 +0300 +++ b/zz_configDialog.cpp Fri Apr 12 00:28:17 2013 +0300 @@ -21,6 +21,8 @@ #include "file.h" #include "config.h" #include "misc.h" +#include "colors.h" +#include "zz_colorSelectDialog.h" #include <qgridlayout.h> #include <qfiledialog.h> #include <qcolordialog.h> @@ -35,6 +37,7 @@ extern_cfg (float, gl_maincolor_alpha); extern_cfg (int, gl_linethickness); extern_cfg (int, gui_toolbar_iconsize); +extern_cfg (str, gui_colortoolbar); ConfigDialog* g_ConfigDialog = nullptr; @@ -53,6 +56,7 @@ initMainTab (); initShortcutsTab (); + initQuickColorTab (); IMPLEMENT_DIALOG_BUTTONS @@ -190,6 +194,182 @@ // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= +void ConfigDialog::initQuickColorTab () { + qQuickColorTab = new QWidget; + + qAddColor = new QPushButton (getIcon ("palette"), "Add"); + qDelColor = new QPushButton (getIcon ("delete"), "Remove"); + qChangeColor = new QPushButton (getIcon ("palette"), "Set"); + qAddColorSeparator = new QPushButton ("Add Separator"); + qMoveColorUp = new QPushButton (getIcon ("arrow-up"), "Move Up"); + qMoveColorDown = new QPushButton (getIcon ("arrow-down"), "Move Down"); + qClearColors = new QPushButton (getIcon ("delete-all"), "Clear"); + + qQuickColorList = new QListWidget; + + quickColorMeta = parseQuickColorMeta (); + updateQuickColorList (); + + QVBoxLayout* qButtonLayout = new QVBoxLayout; + qButtonLayout->addWidget (qAddColor); + qButtonLayout->addWidget (qDelColor); + qButtonLayout->addWidget (qChangeColor); + qButtonLayout->addWidget (qAddColorSeparator); + qButtonLayout->addWidget (qMoveColorUp); + qButtonLayout->addWidget (qMoveColorDown); + qButtonLayout->addWidget (qClearColors); + qButtonLayout->addStretch (1); + + connect (qAddColor, SIGNAL (clicked ()), this, SLOT (slot_setColor ())); + connect (qDelColor, SIGNAL (clicked ()), this, SLOT (slot_delColor ())); + connect (qChangeColor, SIGNAL (clicked ()), this, SLOT (slot_setColor ())); + connect (qAddColorSeparator, SIGNAL (clicked ()), this, SLOT (slot_addColorSeparator ())); + connect (qMoveColorUp, SIGNAL (clicked ()), this, SLOT (slot_moveColor ())); + connect (qMoveColorDown, SIGNAL (clicked ()), this, SLOT (slot_moveColor ())); + connect (qClearColors, SIGNAL (clicked ()), this, SLOT (slot_clearColors ())); + + QGridLayout* qLayout = new QGridLayout; + qLayout->addWidget (qQuickColorList, 0, 0); + qLayout->addLayout (qButtonLayout, 0, 1); + + qQuickColorTab->setLayout (qLayout); + qTabs->addTab (qQuickColorTab, "Quick Colors"); +} + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +void ConfigDialog::updateQuickColorList (quickColorMetaEntry* pSel) { + for (QListWidgetItem* qItem : qaQuickColorItems) + delete qItem; + + qaQuickColorItems.clear (); + + // Init table items + for (quickColorMetaEntry& entry : quickColorMeta) { + QListWidgetItem* qItem = new QListWidgetItem; + + if (entry.bSeparator) { + qItem->setText ("--------"); + qItem->setIcon (getIcon ("empty")); + } else { + color* col = entry.col; + + if (col == nullptr) { + qItem->setText ("[[unknown color]]"); + qItem->setIcon (getIcon ("error")); + } else { + qItem->setText (col->zName); + qItem->setIcon (getIcon ("palette")); + } + } + + qQuickColorList->addItem (qItem); + qaQuickColorItems.push_back (qItem); + + if (pSel && &entry == pSel) { + qQuickColorList->setCurrentItem (qItem); + qQuickColorList->scrollToItem (qItem); + } + } +} + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +void ConfigDialog::slot_setColor () { + quickColorMetaEntry* pEntry = nullptr; + QListWidgetItem* qItem = nullptr; + const bool bNew = static_cast<QPushButton*> (sender ()) == qAddColor; + + if (bNew == false) { + qItem = getSelectedQuickColor (); + if (!qItem) + return; + + ulong ulIdx = getItemRow (qItem, qaQuickColorItems); + pEntry = &quickColorMeta[ulIdx]; + + if (pEntry->bSeparator == true) + return; // don't color separators + } + + short dDefault = pEntry ? pEntry->col->index () : -1; + short dValue; + + if (ColorSelectDialog::staticDialog (dValue, dDefault, this) == false) + return; + + if (pEntry) + pEntry->col = getColor (dValue); + else { + quickColorMetaEntry entry = {getColor (dValue), nullptr, false}; + + qItem = getSelectedQuickColor (); + ulong idx; + + if (qItem) + idx = getItemRow (qItem, qaQuickColorItems) + 1; + else + idx = qaQuickColorItems.size(); + + quickColorMeta.insert (quickColorMeta.begin() + idx, entry); + pEntry = &quickColorMeta[idx]; + } + + updateQuickColorList (pEntry); +} + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +void ConfigDialog::slot_delColor () { + if (qQuickColorList->selectedItems().size() == 0) + return; + + QListWidgetItem* qItem = qQuickColorList->selectedItems ()[0]; + ulong ulIdx = getItemRow (qItem, qaQuickColorItems); + quickColorMeta.erase (quickColorMeta.begin () + ulIdx); + updateQuickColorList (); +} + +// ============================================================================= +void ConfigDialog::slot_moveColor () { + const bool bUp = (static_cast<QPushButton*> (sender()) == qMoveColorUp); + + if (qQuickColorList->selectedItems().size() == 0) + return; + + QListWidgetItem* qItem = qQuickColorList->selectedItems ()[0]; + ulong ulIdx = getItemRow (qItem, qaQuickColorItems); + + long lDest = bUp ? (ulIdx - 1) : (ulIdx + 1); + + if (lDest < 0 || (ulong)lDest >= qaQuickColorItems.size ()) + return; // destination out of bounds + + quickColorMetaEntry tmp = quickColorMeta[lDest]; + quickColorMeta[lDest] = quickColorMeta[ulIdx]; + quickColorMeta[ulIdx] = tmp; + + updateQuickColorList (&quickColorMeta[lDest]); +} + +// ============================================================================= +void ConfigDialog::slot_addColorSeparator() { + quickColorMeta.push_back ({nullptr, nullptr, true}); + updateQuickColorList (&quickColorMeta[quickColorMeta.size () - 1]); +} + +// ============================================================================= +void ConfigDialog::slot_clearColors () { + quickColorMeta.clear (); + updateQuickColorList (); +} + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= void ConfigDialog::makeSlider (QSlider*& qSlider, short int dMin, short int dMax, short dDefault) { @@ -254,10 +434,10 @@ } // ============================================================================= -long ConfigDialog::getItemRow (QListWidgetItem* qItem) { +long ConfigDialog::getItemRow (QListWidgetItem* qItem, std::vector<QListWidgetItem*>& haystack) { long i = 0; - for (QListWidgetItem* it : qaShortcutItems) { + for (QListWidgetItem* it : haystack) { if (it == qItem) return i; ++i; @@ -267,6 +447,14 @@ } // ============================================================================= +QListWidgetItem* ConfigDialog::getSelectedQuickColor () { + if (qQuickColorList->selectedItems().size() == 0) + return nullptr; + + return qQuickColorList->selectedItems ()[0]; +} + +// ============================================================================= void ConfigDialog::slot_setShortcut () { QList<QListWidgetItem*> qaSel = qShortcutList->selectedItems (); @@ -276,7 +464,7 @@ QListWidgetItem* qItem = qaSel[0]; // Find the row this object is on. - long idx = getItemRow (qItem); + long idx = getItemRow (qItem, qaShortcutItems); if (KeySequenceDialog::staticDialog (g_ActionMeta[idx], this)) setShortcutText (qItem, g_ActionMeta[idx]); @@ -287,7 +475,7 @@ QList<QListWidgetItem*> qaSel = qShortcutList->selectedItems (); for (QListWidgetItem* qItem : qaSel) { - long idx = getItemRow (qItem); + long idx = getItemRow (qItem, qaShortcutItems); actionmeta meta = g_ActionMeta[idx]; keyseqconfig* conf = g_ActionMeta[idx].conf; @@ -305,7 +493,7 @@ QKeySequence qDummySeq; for (QListWidgetItem* qItem : qaSel) { - long idx = getItemRow (qItem); + long idx = getItemRow (qItem, qaShortcutItems); actionmeta meta = g_ActionMeta[idx]; keyseqconfig* conf = g_ActionMeta[idx].conf; @@ -330,8 +518,27 @@ // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= -void ConfigDialog::staticDialog (ForgeWindow* window) { - ConfigDialog dlg (window); +str ConfigDialog::makeColorToolBarString () { + str zVal; + + for (quickColorMetaEntry entry : quickColorMeta) { + if (~zVal > 0) + zVal += ':'; + + if (entry.bSeparator) + zVal += '|'; + else + zVal.appendformat ("%d", entry.col->index ()); + } + + return zVal; +} + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +void ConfigDialog::staticDialog () { + ConfigDialog dlg (g_ForgeWindow); if (dlg.exec ()) { io_ldpath = dlg.qLDrawPath->text(); @@ -343,21 +550,29 @@ gl_linethickness = dlg.qGLLineThickness->value (); gui_toolbar_iconsize = (dlg.qToolBarIconSize->value () * 4) + 12; + // Manage the quick color toolbar + g_ForgeWindow->quickColorMeta = dlg.quickColorMeta; + gui_colortoolbar = dlg.makeColorToolBarString (); + // Save the config config::save (); // Reload all subfiles reloadAllSubfiles (); - window->R->setBackground (); - window->refresh (); - window->updateToolBars (); + g_ForgeWindow->R->setBackground (); + g_ForgeWindow->refresh (); + g_ForgeWindow->updateToolBars (); } } -// ============================================================================= -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -// ============================================================================= +// ========================================================================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ========================================================================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ========================================================================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ========================================================================================================================= KeySequenceDialog::KeySequenceDialog (QKeySequence seq, QWidget* parent, Qt::WindowFlags f) : QDialog (parent, f), seq (seq) {
--- a/zz_configDialog.h Thu Apr 11 17:15:01 2013 +0300 +++ b/zz_configDialog.h Fri Apr 12 00:28:17 2013 +0300 @@ -30,7 +30,7 @@ public: QTabWidget* qTabs; - QWidget* qMainTab, *qShortcutsTab; + QWidget* qMainTab, *qShortcutsTab, *qQuickColorTab; // ========================================================================= // Main tab widgets @@ -49,29 +49,49 @@ QPushButton* qSetShortcut, *qResetShortcut, *qClearShortcut; std::vector<QListWidgetItem*> qaShortcutItems; + // ========================================================================= + // Quick color toolbar tab + QListWidget* qQuickColorList; + QPushButton* qAddColor, *qDelColor, *qChangeColor, *qAddColorSeparator, + *qMoveColorUp, *qMoveColorDown, *qClearColors; + std::vector<QListWidgetItem*> qaQuickColorItems; + std::vector<quickColorMetaEntry> quickColorMeta; + + // ========================================================================= QDialogButtonBox* qButtons; ConfigDialog (ForgeWindow* parent); ~ConfigDialog (); - static void staticDialog (ForgeWindow* window); + static void staticDialog (); private: void initMainTab (); void initShortcutsTab (); + void initQuickColorTab (); void makeSlider (QSlider*& qSlider, short int dMin, short int dMax, short int dDefault); void setButtonBackground (QPushButton* qButton, str zValue); void pickColor (strconfig& cfg, QPushButton* qButton); + void updateQuickColorList (quickColorMetaEntry* pSel = nullptr); void setShortcutText (QListWidgetItem* qItem, actionmeta meta); - long getItemRow (QListWidgetItem* qItem); + long getItemRow (QListWidgetItem* qItem, std::vector<QListWidgetItem*>& haystack); + str makeColorToolBarString (); + QListWidgetItem* getSelectedQuickColor (); private slots: void slot_findLDrawPath (); void slot_setGLBackground (); void slot_setGLForeground (); + void slot_setShortcut (); void slot_resetShortcut (); void slot_clearShortcut (); + + void slot_setColor (); + void slot_delColor (); + void slot_addColorSeparator (); + void slot_moveColor (); + void slot_clearColors (); }; // =============================================================================
--- a/zz_historyDialog.cpp Thu Apr 11 17:15:01 2013 +0300 +++ b/zz_historyDialog.cpp Fri Apr 12 00:28:17 2013 +0300 @@ -238,13 +238,8 @@ // ============================================================================= void HistoryDialog::slot_clear () { - if (QMessageBox::question (this, "Confirm", "Are you sure you want to " - "clear the edit history?\nThis cannot be undone.", - (QMessageBox::Yes | QMessageBox::No), QMessageBox::No) != QMessageBox::Yes) - { - // Canceled - return; - } + if (!confirm ("Are you sure you want to clear the edit history?\nThis cannot be undone.")) + return; // Canceled History::clear (); populateList ();