# HG changeset patch # User Santeri Piippo # Date 1368543650 -10800 # Node ID a027f6fc614110abb4c0c613482958ae0a2a04b6 # Parent 4d620d819f4fd049d005c0f8e556d779b4b91969 Un-templated CheckBoxGroup, moved it and RadioButton into widgets.cpp/widgets.h; removed license text tab because I think LICENSE is enough. diff -r 4d620d819f4f -r a027f6fc6141 ldforge.pro --- a/ldforge.pro Tue May 14 04:17:43 2013 +0300 +++ b/ldforge.pro Tue May 14 18:00:50 2013 +0300 @@ -3,7 +3,7 @@ ###################################################################### TEMPLATE = app -TARGET = +TARGET = ldforge DEPENDPATH += . INCLUDEPATH += . OBJECTS_DIR = ./build/ diff -r 4d620d819f4f -r a027f6fc6141 src/aboutDialog.cpp --- a/src/aboutDialog.cpp Tue May 14 04:17:43 2013 +0300 +++ b/src/aboutDialog.cpp Tue May 14 18:00:50 2013 +0300 @@ -28,113 +28,37 @@ AboutDialog::AboutDialog (QWidget* parent, Qt::WindowFlags f) : QDialog (parent, f) { QWidget* mainTab, *licenseTab; - QTabWidget* tabs = new QTabWidget; + + // Application icon - in full 64 x 64 glory. + QLabel* icon = new QLabel; + icon->setPixmap (getIcon ("ldforge")); - { - mainTab = new QWidget; - - // Application icon - in full 64 x 64 glory. - QLabel* icon = new QLabel; - icon->setPixmap (getIcon ("ldforge")); - - // Heading - application label and copyright information - QLabel* title = new QLabel (fmt ("" APPNAME " v%d.%d
" - "Copyright (C) 2013 Santeri Piippo", - VERSION_MAJOR, VERSION_MINOR)); - - // Body text - QLabel* info = new QLabel ( - "

This software is intended for usage as a parts
" - "authoring tool for the LDraw parts library.

" - - "

" APPNAME " is free software, and you are welcome
" - "to redistribute it under the terms of GPL v3. See the LICENSE
" - "text file or the license tab in this dialog for details. If the
" - "license text is not available for some reason, see
" - "http://www.gnu.org/licenses/" - "for the license terms.

" - - "

The application icon is derived from " - "this image.

" - ); - - // Rest in peace, James. - QLabel* memorial = new QLabel ("In living memory of James Jessiman."); - - QVBoxLayout* layout = new QVBoxLayout; - layout->addWidget (icon); - layout->addWidget (title); - layout->addWidget (info); - layout->addWidget (memorial); - - // Align everything to the center. - for (QLabel* label : vector ({icon, title, info, memorial})) - label->setAlignment (Qt::AlignCenter); - - mainTab->setLayout (layout); - tabs->addTab (mainTab, "About " APPNAME); - } + // Heading - application label and copyright information + QLabel* title = new QLabel (fmt ("" APPNAME " v%d.%d
" + "Copyright (C) 2013 Santeri Piippo", + VERSION_MAJOR, VERSION_MINOR)); - { - licenseTab = new QWidget; - - QTextEdit* license = new QTextEdit; - license->setReadOnly (true); + // Body text + QLabel* info = new QLabel ( + "

This software is intended for usage as a parts
" + "authoring tool for the LDraw parts library.

" - QFont font ("Monospace"); - font.setStyleHint (QFont::TypeWriter); - font.setPixelSize (10); - - license->setFont (font); - - // Make the text view wide enough to display the license text. - // Why isn't 80 sufficient here? - license->setMinimumWidth (license->fontMetrics ().width ('a') * 85); - - // Try open the license text - FILE* fp = fopen ("LICENSE", "r"); + "

" APPNAME " is free software, and you are welcome
" + "to redistribute it under the terms of GPL v3. See the
" + "LICENSE text file for details. If the license text is not
" + "available for some reason, see
" + "http://www.gnu.org/licenses/ for the license terms.

" - if (fp == null) { - // Failed; tell the user how to get the license text instead. - setlocale (LC_ALL, "C"); - char const* text = "Couldn't open LICENSE: %s.
" - "See http://www.gnu.org/licenses/ for the GPLv3 text."; - - license->setHtml (fmt (text, strerror (errno))); - } else { - // Figure out file size - fseek (fp, 0, SEEK_END); - const size_t length = ftell (fp); - rewind (fp); - - // Init text buffer and write pointer - char* licenseText = new char[length]; - char* writePtr = &licenseText[0]; - - // Read in the license text - while (true) { - *writePtr = fgetc (fp); - - if (feof (fp)) - break; - - writePtr++; - } - - // Add terminating null character and add the license text to the - // license dialog text view. - *writePtr = '\0'; - license->setText (licenseText); - - // And dump the trash on the way out. - delete[] licenseText; - } - - QVBoxLayout* layout = new QVBoxLayout; - layout->addWidget (license); - licenseTab->setLayout (layout); - tabs->addTab (licenseTab, "License"); - } + "

The application icon is derived from " + "this image.

" + ); + + // Rest in peace, James. + QLabel* memorial = new QLabel ("In living memory of James Jessiman."); + + // Align everything to the center. + for (QLabel* label : vector ({icon, title, info, memorial})) + label->setAlignment (Qt::AlignCenter); QDialogButtonBox* buttons = new QDialogButtonBox (QDialogButtonBox::Close); QPushButton* helpButton = new QPushButton; @@ -145,10 +69,13 @@ connect (buttons, SIGNAL (helpRequested ()), this, SLOT (slot_mail ())); connect (buttons, SIGNAL (rejected ()), this, SLOT (reject ())); - QVBoxLayout* layout = new QVBoxLayout; - layout->addWidget (tabs); + QVBoxLayout* layout = new QVBoxLayout (this); + layout->addWidget (icon); + layout->addWidget (title); + layout->addWidget (info); + layout->addWidget (memorial); layout->addWidget (buttons); - setLayout (layout); + setWindowTitle ("About " APPNAME); } diff -r 4d620d819f4f -r a027f6fc6141 src/addObjectDialog.cpp --- a/src/addObjectDialog.cpp Tue May 14 04:17:43 2013 +0300 +++ b/src/addObjectDialog.cpp Tue May 14 18:00:50 2013 +0300 @@ -31,7 +31,7 @@ #include "colors.h" #include "colorSelectDialog.h" #include "history.h" -#include "radiobox.h" +#include "widgets.h" #define APPLY_COORDS(OBJ, N) \ for (short i = 0; i < N; ++i) \ @@ -87,7 +87,7 @@ rb_bfcType = new RadioBox ("Statement", {}, 0, Qt::Vertical); for (int i = 0; i < LDBFC::NumStatements; ++i) - rb_bfcType->addButton (new QRadioButton (LDBFC::statements[i])); + rb_bfcType->addButton (LDBFC::statements[i]); if (obj) rb_bfcType->setValue ((int) static_cast (obj)->type); @@ -161,10 +161,10 @@ if (i % (LDRadial::NumTypes / 2) == 0) rb_radType->rowBreak (); - rb_radType->addButton (new QRadioButton (LDRadial::radialTypeName ((LDRadial::Type) i))); + rb_radType->addButton (LDRadial::radialTypeName ((LDRadial::Type) i)); } - connect (rb_radType, SIGNAL (sig_buttonPressed (int)), this, SLOT (slot_radialTypeChanged (int))); + connect (rb_radType, SIGNAL (buttonPressed (int)), this, SLOT (slot_radialTypeChanged (int))); cb_radHiRes = new QCheckBox ("Hi-Res"); diff -r 4d620d819f4f -r a027f6fc6141 src/checkboxgroup.h --- a/src/checkboxgroup.h Tue May 14 04:17:43 2013 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,40 +0,0 @@ -#include -#include -#include -#include - -QBoxLayout::Direction makeDirection (Qt::Orientation orient, bool invert = false); - -template class CheckBoxGroup : public QGroupBox { -public: - CheckBoxGroup (const char* label, Qt::Orientation orient = Qt::Horizontal, QWidget* parent = null) : QGroupBox (parent) { - m_layout = new QBoxLayout (makeDirection (orient)); - setTitle (label); - setLayout (m_layout); - } - - void addCheckBox (const char* label, T key, bool checked = false) { - if (m_vals.find (key) != m_vals.end ()) - return; - - QCheckBox* box = new QCheckBox (label); - box->setChecked (checked); - - m_vals[key] = box; - m_layout->addWidget (box); - } - - std::vector checkedValues () const { - std::vector vals; - - for (const auto& kv : m_vals) - if (kv.second->isChecked ()) - vals.push_back (kv.first); - - return vals; - } - -private: - QBoxLayout* m_layout; - std::map m_vals; -}; \ No newline at end of file diff -r 4d620d819f4f -r a027f6fc6141 src/colorSelectDialog.cpp --- a/src/colorSelectDialog.cpp Tue May 14 04:17:43 2013 +0300 +++ b/src/colorSelectDialog.cpp Tue May 14 18:00:50 2013 +0300 @@ -22,7 +22,9 @@ #include #include #include -#include +#include +#include + #include "common.h" #include "gui.h" #include "colorSelectDialog.h" @@ -30,12 +32,12 @@ #include "config.h" #include "misc.h" -static const short g_dNumColumns = 8; -static const short g_dNumRows = 10; -static const short g_dSquareSize = 32; -static const long g_lWidth = (g_dNumColumns * g_dSquareSize); -static const long g_lHeight = (g_dNumRows * g_dSquareSize); -static const long g_lMaxHeight = ((MAX_COLORS / g_dNumColumns) * g_dSquareSize); +static const short g_numCols = 8; +static const short g_numRows = 10; +static const short g_squareSize = 32; +static const long g_width = (g_numCols * g_squareSize); +static const long g_height = (g_numRows * g_squareSize); +static const long g_maxHeight = ((MAX_COLORS / g_numCols) * g_squareSize); extern_cfg (str, gl_maincolor); extern_cfg (float, gl_maincolor_alpha); @@ -55,36 +57,34 @@ // not really an icon but eh gs_scene->setBackgroundBrush (getIcon ("checkerboard")); - gs_scene->setSceneRect (0, 0, g_lWidth, g_lMaxHeight); - gv_view->setSceneRect (0, 0, g_lWidth, g_lMaxHeight); + gs_scene->setSceneRect (0, 0, g_width, g_maxHeight); + gv_view->setSceneRect (0, 0, g_width, g_maxHeight); drawScene (); - IMPLEMENT_DIALOG_BUTTONS - // Set the size of the view - const long lWidth = g_lWidth + 21; // HACK - gv_view->setMaximumWidth (lWidth); - gv_view->setMinimumWidth (lWidth); - gv_view->setMaximumHeight (g_lHeight); - gv_view->setMinimumHeight (g_lHeight); + const long viewWidth = g_width + 21; // HACK: 21 for scrollbar + gv_view->setMaximumWidth (viewWidth); + gv_view->setMinimumWidth (viewWidth); + gv_view->setMaximumHeight (g_height); + gv_view->setMinimumHeight (g_height); gv_view->setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOff); // If we have a default color selected, scroll down so that it is visible. - // TODO: find a better way to do this - if (defval >= ((g_dNumColumns * g_dNumRows) - 2)) { - ulong ulNewY = ((defval / g_dNumColumns) - 3) * g_dSquareSize; + // HACK: find a better way to do this + if (defval >= ((g_numCols * g_numRows) - 2)) { + ulong ulNewY = ((defval / g_numCols) - 3) * g_squareSize; gv_view->verticalScrollBar ()->setSliderPosition (ulNewY); } lb_colorInfo = new QLabel; drawColorInfo (); - QVBoxLayout* qLayout = new QVBoxLayout; - qLayout->addWidget (gv_view); - qLayout->addWidget (lb_colorInfo); - qLayout->addWidget (bbx_buttons); - setLayout (qLayout); + QVBoxLayout* layout = new QVBoxLayout; + layout->addWidget (gv_view); + layout->addWidget (lb_colorInfo); + layout->addWidget (makeButtonBox (*this)); + setLayout (layout); setWindowIcon (getIcon ("palette")); setWindowTitle (APPNAME); @@ -94,8 +94,8 @@ // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= void ColorSelectDialog::drawScene () { - const double fPenWidth = 1.0f; - QPen qPen (Qt::black, fPenWidth, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin); + const double penWidth = 1.0f; + QPen pen (Qt::black, penWidth, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin); // Draw the color rectangles. gs_scene->clear (); @@ -104,9 +104,9 @@ if (!meta) continue; - const double x = (i % g_dNumColumns) * g_dSquareSize; - const double y = (i / g_dNumColumns) * g_dSquareSize; - const double w = (g_dSquareSize) - (fPenWidth / 2); + const double x = (i % g_numCols) * g_squareSize; + const double y = (i / g_numCols) * g_squareSize; + const double w = (g_squareSize) - (penWidth / 2); QColor col = meta->faceColor; @@ -116,12 +116,10 @@ col.setAlpha (gl_maincolor_alpha * 255.0f); } - bool dark = (luma (col) < 80); - - gs_scene->addRect (x, y, w, w, qPen, col); - QGraphicsTextItem* qText = gs_scene->addText (fmt ("%lu", i).chars()); - qText->setDefaultTextColor ((dark) ? Qt::white : Qt::black); - qText->setPos (x, y); + gs_scene->addRect (x, y, w, w, pen, col); + QGraphicsTextItem* numtext = gs_scene->addText (fmt ("%d", i).chars()); + numtext->setDefaultTextColor ((luma (col) < 80) ? Qt::white : Qt::black); + numtext->setPos (x, y); if (i == selColor) { auto curspic = gs_scene->addPixmap (getIcon ("colorcursor")); @@ -149,11 +147,11 @@ // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= void ColorSelectDialog::mousePressEvent (QMouseEvent* event) { - QPointF qPoint = gv_view->mapToScene (event->pos ()); + QPointF scenepos = gv_view->mapToScene (event->pos ()); - ulong x = ((ulong)qPoint.x () - (g_dSquareSize / 2)) / g_dSquareSize; - ulong y = ((ulong)qPoint.y () - (g_dSquareSize / 2)) / g_dSquareSize; - ulong idx = (y * g_dNumColumns) + x; + ulong x = ((ulong) scenepos.x () - (g_squareSize / 2)) / g_squareSize; + ulong y = ((ulong) scenepos.y () - (g_squareSize / 2)) / g_squareSize; + ulong idx = (y * g_numCols) + x; color* col = getColor (idx); if (!col) diff -r 4d620d819f4f -r a027f6fc6141 src/colorSelectDialog.h --- a/src/colorSelectDialog.h Tue May 14 04:17:43 2013 +0300 +++ b/src/colorSelectDialog.h Tue May 14 18:00:50 2013 +0300 @@ -19,12 +19,12 @@ #ifndef COLORSELECTOR_H #define COLORSELECTOR_H -#include -#include -#include -#include +#include #include "common.h" +class QLabel; +class QGraphicsView; +class QGraphicsScene; class ColorSelectDialog : public QDialog { Q_OBJECT @@ -35,7 +35,6 @@ QGraphicsScene* gs_scene; QGraphicsView* gv_view; QLabel* lb_colorInfo; - QDialogButtonBox* bbx_buttons; short selColor; private: diff -r 4d620d819f4f -r a027f6fc6141 src/configDialog.cpp --- a/src/configDialog.cpp Tue May 14 04:17:43 2013 +0300 +++ b/src/configDialog.cpp Tue May 14 18:00:50 2013 +0300 @@ -16,12 +16,13 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include + #include "common.h" #include "configDialog.h" #include "file.h" @@ -758,11 +759,14 @@ // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= void KeySequenceDialog::updateOutput () { - str zShortcut = seq.toString (); + str shortcut = seq.toString (); - str zText = fmt ("
%s
", zShortcut.chars ()); + if (seq == QKeySequence ()) + shortcut = "<empty>"; - lb_output->setText (zText); + str text = fmt ("
%s
", shortcut.chars ()); + + lb_output->setText (text); } // ============================================================================= diff -r 4d620d819f4f -r a027f6fc6141 src/dialogs.cpp --- a/src/dialogs.cpp Tue May 14 04:17:43 2013 +0300 +++ b/src/dialogs.cpp Tue May 14 18:00:50 2013 +0300 @@ -23,13 +23,13 @@ #include #include #include +#include #include "dialogs.h" -#include "radiobox.h" +#include "widgets.h" #include "gui.h" #include "gldraw.h" #include "docs.h" -#include "checkboxgroup.h" #include "file.h" #include "dialogs.h" @@ -176,15 +176,15 @@ setLayout (layout); } -double ReplaceCoordsDialog::searchValue() const { +double ReplaceCoordsDialog::searchValue () const { return dsb_search->value (); } -double ReplaceCoordsDialog::replacementValue() const { +double ReplaceCoordsDialog::replacementValue () const { return dsb_replacement->value (); } -vector ReplaceCoordsDialog::axes() const { +vector ReplaceCoordsDialog::axes () const { return cbg_axes->checkedValues (); } diff -r 4d620d819f4f -r a027f6fc6141 src/dialogs.h --- a/src/dialogs.h Tue May 14 04:17:43 2013 +0300 +++ b/src/dialogs.h Tue May 14 18:00:50 2013 +0300 @@ -10,7 +10,7 @@ class QLineEdit; class QSpinBox; class RadioBox; -template class CheckBoxGroup; +class CheckBoxGroup; class QLabel; class QAbstractButton; @@ -46,12 +46,12 @@ public: explicit ReplaceCoordsDialog (QWidget* parent = null, Qt::WindowFlags f = 0); - vector axes () const; + std::vector< int > axes () const; double searchValue () const; double replacementValue () const; private: - CheckBoxGroup* cbg_axes; + CheckBoxGroup* cbg_axes; QLabel* lb_search, *lb_replacement; QDoubleSpinBox* dsb_search, *dsb_replacement; }; diff -r 4d620d819f4f -r a027f6fc6141 src/extprogs.cpp --- a/src/extprogs.cpp Tue May 14 04:17:43 2013 +0300 +++ b/src/extprogs.cpp Tue May 14 18:00:50 2013 +0300 @@ -16,21 +16,21 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include + #include "common.h" #include "config.h" #include "misc.h" #include "extprogs.h" #include "gui.h" #include "file.h" -#include "radiobox.h" +#include "widgets.h" #include "history.h" #include "labeledwidget.h" diff -r 4d620d819f4f -r a027f6fc6141 src/file.cpp --- a/src/file.cpp Tue May 14 04:17:43 2013 +0300 +++ b/src/file.cpp Tue May 14 18:00:50 2013 +0300 @@ -18,6 +18,7 @@ #include #include + #include "common.h" #include "config.h" #include "file.h" diff -r 4d620d819f4f -r a027f6fc6141 src/gui.cpp --- a/src/gui.cpp Tue May 14 04:17:43 2013 +0300 +++ b/src/gui.cpp Tue May 14 18:00:50 2013 +0300 @@ -38,7 +38,7 @@ #include "misc.h" #include "colors.h" #include "history.h" -#include "checkboxgroup.h" +#include "widgets.h" #include "config.h" vector g_ActionMeta; @@ -1043,8 +1043,8 @@ return bbx_buttons; } -CheckBoxGroup* makeAxesBox () { - CheckBoxGroup* cbg_axes = new CheckBoxGroup ("Axes", Qt::Horizontal); +CheckBoxGroup* makeAxesBox () { + CheckBoxGroup* cbg_axes = new CheckBoxGroup ("Axes", Qt::Horizontal); cbg_axes->addCheckBox ("X", X); cbg_axes->addCheckBox ("Y", Y); cbg_axes->addCheckBox ("Z", Z); diff -r 4d620d819f4f -r a027f6fc6141 src/gui.h --- a/src/gui.h Tue May 14 04:17:43 2013 +0300 +++ b/src/gui.h Tue May 14 18:00:50 2013 +0300 @@ -33,7 +33,7 @@ class QToolButton; class QDialogButtonBox; class GLRenderer; -template class CheckBoxGroup; +class CheckBoxGroup; // Stuff for dialogs #define IMPLEMENT_DIALOG_BUTTONS \ @@ -193,7 +193,7 @@ QIcon makeColorIcon (color* colinfo, const ushort size); void makeColorSelector (QComboBox* box); QDialogButtonBox* makeButtonBox (QDialog& dlg); -CheckBoxGroup* makeAxesBox (); +CheckBoxGroup* makeAxesBox (); // ----------------------------------------------------------------------------- // Pointer to the instance of ForgeWindow. diff -r 4d620d819f4f -r a027f6fc6141 src/gui_actions.cpp --- a/src/gui_actions.cpp Tue May 14 04:17:43 2013 +0300 +++ b/src/gui_actions.cpp Tue May 14 18:00:50 2013 +0300 @@ -20,6 +20,8 @@ #include #include #include +#include +#include #include "gui.h" #include "file.h" diff -r 4d620d819f4f -r a027f6fc6141 src/gui_editactions.cpp --- a/src/gui_editactions.cpp Tue May 14 04:17:43 2013 +0300 +++ b/src/gui_editactions.cpp Tue May 14 18:00:50 2013 +0300 @@ -18,6 +18,7 @@ #include #include +#include #include "gui.h" #include "common.h" @@ -27,9 +28,8 @@ #include "historyDialog.h" #include "misc.h" #include "bbox.h" -#include "radiobox.h" +#include "widgets.h" #include "extprogs.h" -#include "checkboxgroup.h" #include "gldraw.h" #include "dialogs.h" @@ -123,7 +123,7 @@ // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= -static void doInline (bool bDeep) { +static void doInline (bool deep) { vector sel = g_win->sel (); // History stuff @@ -148,7 +148,7 @@ vector objs; if (obj->getType() == LDObject::Subfile) - objs = static_cast (obj)->inlineContents (bDeep, true); + objs = static_cast (obj)->inlineContents (deep, true); else if (obj->getType() == LDObject::Radial) objs = static_cast (obj)->decompose (true); else @@ -169,7 +169,7 @@ delete obj; } - History::addEntry (new InlineHistory (bitIndices, refIndices, refs, bDeep)); + History::addEntry (new InlineHistory (bitIndices, refIndices, refs, deep)); g_win->fullRefresh (); } @@ -689,7 +689,7 @@ const double search = dlg.searchValue (), replacement = dlg.replacementValue (); - vector sel = dlg.axes (); + vector sel = dlg.axes (); EditHistory* history = new EditHistory; @@ -697,10 +697,11 @@ LDObject* copy = obj->clone (); for (short i = 0; i < obj->vertices (); ++i) - for (Axis ax : sel) { - if (obj->coords[i][ax] == search) { - obj->coords[i][ax] = replacement; - } + for (int ax : sel) { + double& coord = obj->coords[i][(Axis) ax]; + + if (coord == search) + coord = replacement; } history->addEntry (copy, obj, obj->getIndex (g_curfile)); @@ -726,10 +727,10 @@ setLayout (layout); } - vector axes () { return cbg_axes->checkedValues (); } + vector axes () { return cbg_axes->checkedValues (); } private: - CheckBoxGroup* cbg_axes; + CheckBoxGroup* cbg_axes; }; MAKE_ACTION (flip, "Flip", "flip", "Flip coordinates", CTRL_SHIFT (F)) { @@ -739,15 +740,15 @@ return; EditHistory* history = new EditHistory; - vector sel = dlg.axes (); + vector sel = dlg.axes (); for (LDObject* obj : g_win->sel ()) { bool altered = false; LDObject* copy = obj->clone (); for (short i = 0; i < obj->vertices (); ++i) - for (Axis ax : sel) { - obj->coords[i][ax] *= -1; + for (int ax : sel) { + obj->coords[i][(Axis) ax] *= -1; altered = true; } diff -r 4d620d819f4f -r a027f6fc6141 src/radiobox.cpp --- a/src/radiobox.cpp Tue May 14 04:17:43 2013 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,104 +0,0 @@ -/* - * LDForge: LDraw parts authoring CAD - * Copyright (C) 2013 Santeri Piippo - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include -#include -#include "radiobox.h" - -QBoxLayout::Direction makeDirection (Qt::Orientation orient, bool invert = false) { - return (orient == (invert ? Qt::Vertical : Qt::Horizontal)) ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom; -} - -void RadioBox::init (Qt::Orientation orient) { - m_dir = makeDirection (orient); - - m_buttonGroup = new QButtonGroup; - m_oldId = m_curId = 0; - m_coreLayout = null; - - m_coreLayout = new QBoxLayout (makeDirection (orient, true)); - setLayout (m_coreLayout); - - // Init the first row with a break - rowBreak (); - - connect (m_buttonGroup, SIGNAL (buttonPressed (int)), this, SLOT (slot_buttonPressed (int))); - connect (m_buttonGroup, SIGNAL (buttonReleased (int)), this, SLOT (slot_buttonReleased (int))); -} - -RadioBox::RadioBox (const QString& title, initlist entries, int const defaultId, - const Qt::Orientation orient, QWidget* parent) : QGroupBox (title, parent), m_defId (defaultId) -{ - init (orient); - m_oldId = m_defId; - - for (char const* entry : entries) - addButton (entry); -} - -void RadioBox::rowBreak () { - QBoxLayout* newLayout = new QBoxLayout (m_dir); - m_currentLayout = newLayout; - m_layouts.push_back (newLayout); - - m_coreLayout->addLayout (newLayout); -} - -void RadioBox::addButton (const char* entry) { - QRadioButton* button = new QRadioButton (entry); - addButton (button); -} - -void RadioBox::addButton (QRadioButton* button) { - bool const selectThis = (m_curId == m_defId); - - m_objects.push_back (button); - m_buttonGroup->addButton (button, m_curId++); - m_currentLayout->addWidget (button); - - if (selectThis) - button->setChecked (true); -} - -RadioBox& RadioBox::operator<< (QRadioButton* button) { - addButton (button); - return *this; -} - -RadioBox& RadioBox::operator<< (const char* entry) { - addButton (entry); - return *this; -} - -void RadioBox::setCurrentRow (uint row) { - m_currentLayout = m_layouts[row]; -} - -void RadioBox::slot_buttonPressed (int btn) { - emit sig_buttonPressed (btn); - - m_oldId = m_buttonGroup->checkedId (); -} - -void RadioBox::slot_buttonReleased (int btn) { - emit buttonReleased (btn); - int newid = m_buttonGroup->checkedId (); - - if (m_oldId != newid) - emit valueChanged (newid); -} \ No newline at end of file diff -r 4d620d819f4f -r a027f6fc6141 src/radiobox.h --- a/src/radiobox.h Tue May 14 04:17:43 2013 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,115 +0,0 @@ -/* - * LDForge: LDraw parts authoring CAD - * Copyright (C) 2013 Santeri Piippo - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef RADIOBOX_H -#define RADIOBOX_H - -#include "common.h" -#include -#include -#include -#include -#include - -// ============================================================================= -// RadioBox -// -// Convenience widget - is a groupbox of radio buttons. -// ============================================================================= -class RadioBox : public QGroupBox { - Q_OBJECT - -public: - void init (Qt::Orientation orient); - - explicit RadioBox (QWidget* parent = null) : QGroupBox (parent) { - init (Qt::Vertical); - } - - explicit RadioBox (const QString& title, QWidget* parent = null) : QGroupBox (title, parent) { - init (Qt::Vertical); - } - - explicit RadioBox () { - init (Qt::Vertical); - } - - explicit RadioBox (const QString& title, initlist entries, int const defaultId, - const Qt::Orientation orient = Qt::Vertical, QWidget* parent = null); - - void rowBreak (); - void setCurrentRow (uint row); - void addButton (const char* entry); - void addButton (QRadioButton* button); - RadioBox& operator<< (QRadioButton* button); - RadioBox& operator<< (const char* entry); - - int value () const { - return m_buttonGroup->checkedId (); - } - - void setValue (int val) { - m_buttonGroup->button (val)->setChecked (true); - } - - std::vector::iterator begin () { - return m_objects.begin (); - } - - std::vector::iterator end () { - return m_objects.end (); - } - - QRadioButton* operator[] (uint n) const { - return m_objects[n]; - } - - bool exclusive () const { - return m_buttonGroup->exclusive (); - } - - void setExclusive (bool val) { - m_buttonGroup->setExclusive (val); - } - - bool isChecked (int n) const { - return m_buttonGroup->checkedId () == n; - } - -signals: - void sig_buttonPressed (int btn); - void buttonReleased (int btn); - void valueChanged (int val); - -private: - std::vector m_objects; - std::vector m_layouts; - QBoxLayout* m_coreLayout; - QBoxLayout* m_currentLayout; - QBoxLayout::Direction m_dir; - int m_curId, m_defId, m_oldId; - QButtonGroup* m_buttonGroup; - - Q_DISABLE_COPY (RadioBox) - -private slots: - void slot_buttonPressed (int btn); - void slot_buttonReleased (int btn); -}; - -#endif // RADIOBOX_H \ No newline at end of file diff -r 4d620d819f4f -r a027f6fc6141 src/widgets.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/widgets.cpp Tue May 14 18:00:50 2013 +0300 @@ -0,0 +1,177 @@ +/* + * LDForge: LDraw parts authoring CAD + * Copyright (C) 2013 Santeri Piippo + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include + +#include "widgets.h" + +RadioBox::RadioBox (const QString& title, QWidget* parent) : QGroupBox (title, parent) { + init (Qt::Vertical); +} + +QBoxLayout::Direction makeDirection (Qt::Orientation orient, bool invert = false) { + return (orient == (invert ? Qt::Vertical : Qt::Horizontal)) ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom; +} + +bool RadioBox::isChecked (int n) const { + return m_buttonGroup->checkedId () == n; +} + +void RadioBox::init (Qt::Orientation orient) { + m_vert = orient == Qt::Vertical; + + m_buttonGroup = new QButtonGroup; + m_oldId = m_curId = 0; + m_coreLayout = null; + + m_coreLayout = new QBoxLayout ((orient == Qt::Vertical) ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom); + setLayout (m_coreLayout); + + // Init the first row with a break + rowBreak (); + + connect (m_buttonGroup, SIGNAL (buttonPressed (int)), this, SLOT (slot_buttonPressed (int))); + connect (m_buttonGroup, SIGNAL (buttonReleased (int)), this, SLOT (slot_buttonReleased (int))); +} + +RadioBox::RadioBox (const QString& title, initlist entries, int const defaultId, + const Qt::Orientation orient, QWidget* parent) : QGroupBox (title, parent), m_defId (defaultId) +{ + init (orient); + m_oldId = m_defId; + + for (char const* entry : entries) + addButton (entry); +} + +void RadioBox::rowBreak () { + QBoxLayout* newLayout = new QBoxLayout (m_vert ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight); + m_currentLayout = newLayout; + m_layouts.push_back (newLayout); + + m_coreLayout->addLayout (newLayout); +} + +void RadioBox::addButton (const char* entry) { + QRadioButton* button = new QRadioButton (entry); + addButton (button); +} + +void RadioBox::addButton (QRadioButton* button) { + bool const selectThis = (m_curId == m_defId); + + m_objects.push_back (button); + m_buttonGroup->addButton (button, m_curId++); + m_currentLayout->addWidget (button); + + if (selectThis) + button->setChecked (true); +} + +RadioBox& RadioBox::operator<< (QRadioButton* button) { + addButton (button); + return *this; +} + +RadioBox& RadioBox::operator<< (const char* entry) { + addButton (entry); + return *this; +} + +void RadioBox::setCurrentRow (uint row) { + m_currentLayout = m_layouts[row]; +} + +int RadioBox::value () const { + return m_buttonGroup->checkedId (); +} + +void RadioBox::setValue (int val) { + m_buttonGroup->button (val)->setChecked (true); +} + +QRadioButton* RadioBox::operator[] (uint n) const { + return m_objects[n]; +} + +void RadioBox::slot_buttonPressed (int btn) { + emit buttonPressed (btn); + + m_oldId = m_buttonGroup->checkedId (); +} + +void RadioBox::slot_buttonReleased (int btn) { + emit buttonReleased (btn); + int newid = m_buttonGroup->checkedId (); + + if (m_oldId != newid) + emit valueChanged (newid); +} + +RadioBox::iter RadioBox::begin() { + return m_objects.begin (); +} + +RadioBox::iter RadioBox::end() { + return m_objects.end (); +} + +CheckBoxGroup::CheckBoxGroup (const char* label, Qt::Orientation orient, QWidget* parent) : QGroupBox (parent) { + m_layout = new QBoxLayout (makeDirection (orient)); + setTitle (label); + setLayout (m_layout); +} + +void CheckBoxGroup::addCheckBox (const char* label, int key, bool checked) { + if (m_vals.find (key) != m_vals.end ()) + return; + + QCheckBox* box = new QCheckBox (label); + box->setChecked (checked); + + m_vals[key] = box; + m_layout->addWidget (box); + + connect (box, SIGNAL (stateChanged (int)), this, SLOT (buttonChanged ())); +} + +std::vector CheckBoxGroup::checkedValues () const { + std::vector vals; + + for (const auto& kv : m_vals) + if (kv.second->isChecked ()) + vals.push_back (kv.first); + + return vals; +} + +QCheckBox* CheckBoxGroup::getCheckBox (int key) { + return m_vals[key]; +} + +void CheckBoxGroup::buttonChanged () { + emit selectionChanged (); +} + +bool CheckBoxGroup::buttonChecked (int key) { + return m_vals[key]->isChecked (); +} \ No newline at end of file diff -r 4d620d819f4f -r a027f6fc6141 src/widgets.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/widgets.h Tue May 14 18:00:50 2013 +0300 @@ -0,0 +1,110 @@ +/* + * LDForge: LDraw parts authoring CAD + * Copyright (C) 2013 Santeri Piippo + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef WIDGETS_H +#define WIDGETS_H + +#include +#include +#include "common.h" +#include "types.h" + +class QCheckBox; +class QButtonGroup; +class QBoxLayout; +class QRadioButton; + +// ============================================================================= +// RadioBox +// +// Convenience widget - is a groupbox of radio buttons. +// ============================================================================= +class RadioBox : public QGroupBox { + Q_OBJECT + +public: + typedef std::vector::iterator iter; + + explicit RadioBox (QWidget* parent = null) : QGroupBox (parent) { init (Qt::Vertical); } + explicit RadioBox () { init (Qt::Vertical); } + explicit RadioBox (const QString& title, QWidget* parent = null); + explicit RadioBox (const QString& title, initlist entries, int const defaultId, + const Qt::Orientation orient = Qt::Vertical, QWidget* parent = null); + + void addButton (const char* entry); + void addButton (QRadioButton* button); + iter begin (); + iter end (); + void init (Qt::Orientation orient); + bool isChecked (int n) const; + void rowBreak (); + void setCurrentRow (uint row); + void setValue (int val); + int value () const; + + QRadioButton* operator[] (uint n) const; + RadioBox& operator<< (QRadioButton* button); + RadioBox& operator<< (const char* entry); + +signals: + void buttonPressed (int btn); + void buttonReleased (int btn); + void valueChanged (int val); + +private: + std::vector m_objects; + std::vector m_layouts; + QBoxLayout* m_coreLayout; + QBoxLayout* m_currentLayout; + bool m_vert; + int m_curId, m_defId, m_oldId; + QButtonGroup* m_buttonGroup; + + Q_DISABLE_COPY (RadioBox) + +private slots: + void slot_buttonPressed (int btn); + void slot_buttonReleased (int btn); +}; + +// ============================================================================= +// CheckBoxGroup +// ============================================================================= +class CheckBoxGroup : public QGroupBox { + Q_OBJECT + +public: + CheckBoxGroup (const char* label, Qt::Orientation orient = Qt::Horizontal, QWidget* parent = null); + + void addCheckBox (const char* label, int key, bool checked = false); + vector checkedValues () const; + QCheckBox* getCheckBox (int key); + bool buttonChecked (int key); + +signals: + void selectionChanged (); + +private: + QBoxLayout* m_layout; + std::map m_vals; + +private slots: + void buttonChanged (); +}; + +#endif // WIDGETS_H \ No newline at end of file