# HG changeset patch # User Teemu Piippo # Date 1407149034 -10800 # Node ID a496e72af069c010f35f2a0a537cda2fc76dcabe # Parent ebc7a186699c6441308a3d0ce60ba6a981a98d07 - more refactor, updated .hgignore diff -r ebc7a186699c -r a496e72af069 .hgignore --- a/.hgignore Mon Aug 04 03:35:25 2014 +0300 +++ b/.hgignore Mon Aug 04 13:43:54 2014 +0300 @@ -14,3 +14,5 @@ .kdev_include_paths *.cfg .kdev4 +.*~ +*.orig diff -r ebc7a186699c -r a496e72af069 src/actions.cc --- a/src/actions.cc Mon Aug 04 03:35:25 2014 +0300 +++ b/src/actions.cc Mon Aug 04 13:43:54 2014 +0300 @@ -807,19 +807,15 @@ // Determine the BFC winding type used in the main document - it is to // be carried over to the subfile. - for (LDObjectPtr obj : getCurrentDocument()->objects()) + LDIterate (getCurrentDocument()->objects(), [&] (LDBFCPtr const& bfc) { - if (obj->type() != OBJ_BFC) - continue; - - BFCStatement a = obj.staticCast()->statement(); - - if (eq (a, BFCStatement::CertifyCCW, BFCStatement::CertifyCW, BFCStatement::NoCertify)) + if (eq (bfc->statement(), BFCStatement::CertifyCCW, BFCStatement::CertifyCW, + BFCStatement::NoCertify)) { - bfctype = a; - break; + bfctype = bfc->statement(); + Break(); } - } + }); // Get the body of the document in LDraw code for (LDObjectPtr obj : selection()) diff -r ebc7a186699c -r a496e72af069 src/actionsEdit.cc --- a/src/actionsEdit.cc Mon Aug 04 03:35:25 2014 +0300 +++ b/src/actionsEdit.cc Mon Aug 04 13:43:54 2014 +0300 @@ -121,16 +121,14 @@ { LDObjectList sel = selection(); - for (LDObjectPtr obj : sel) + LDIterate (selection(), [&](LDSubfilePtr const& ref) { // Get the index of the subfile so we know where to insert the // inlined contents. - long idx = obj->lineNumber(); + long idx = ref->lineNumber(); - if (idx == -1 or obj->type() != OBJ_Subfile) - continue; - - LDObjectList objs = obj.staticCast()->inlineContents (deep, false); + assert (idx != -1); + LDObjectList objs = ref->inlineContents (deep, false); // Merge in the inlined objects for (LDObjectPtr inlineobj : objs) @@ -143,8 +141,8 @@ } // Delete the subfile now as it's been inlined. - obj->destroy(); - } + ref->destroy(); + }); g_win->refresh(); } @@ -165,25 +163,22 @@ { int num = 0; - for (LDObjectPtr obj : selection()) + LDIterate (selection(), [&](LDQuadPtr const& quad) { - if (obj->type() != OBJ_Quad) - continue; - // Find the index of this quad - long index = obj->lineNumber(); + long index = quad->lineNumber(); if (index == -1) return; - QList triangles = obj.staticCast()->splitToTriangles(); + QList triangles = quad->splitToTriangles(); // Replace the quad with the first triangle and add the second triangle // after the first one. getCurrentDocument()->setObject (index, triangles[0]); getCurrentDocument()->insertObj (index + 1, triangles[1]); num++; - } + }); print ("%1 quadrilaterals split", num); refresh(); @@ -666,17 +661,13 @@ // void MainWindow::slot_actionDemote() { - LDObjectList sel = selection(); int num = 0; - for (LDObjectPtr obj : sel) + LDIterate (selection(), [&](LDCondLinePtr const& cnd) { - if (obj->type() != OBJ_CondLine) - continue; - - obj.staticCast()->toEdgeLine(); + cnd->toEdgeLine(); ++num; - } + }); print (tr ("Demoted %1 conditional lines"), num); refresh(); @@ -702,8 +693,11 @@ int colnum = 0; LDColor color; - for (colnum = 0; colnum < numLDConfigColors() and ((color = LDColor::fromIndex (colnum)) == null or isColorUsed (color)); colnum++) - ; + for (colnum = 0; + colnum < numLDConfigColors() and + ((color = LDColor::fromIndex (colnum)) == null or + isColorUsed (color)); + colnum++) {} if (colnum >= numLDConfigColors()) { @@ -794,7 +788,7 @@ for (LDObjectPtr obj : selection()) { - if (obj->type() != OBJ_Line and obj->type() != OBJ_CondLine) + if (not eq (obj->type(), OBJ_Line, OBJ_CondLine)) continue; QVector newsegs; @@ -803,8 +797,18 @@ { LDObjectPtr segment; Vertex v0, v1; - v0.apply ([&](Axis ax, double& a) { a = (obj->vertex (0)[ax] + (((obj->vertex (1)[ax] - obj->vertex (0)[ax]) * i) / segments)); }); - v1.apply ([&](Axis ax, double& a) { a = (obj->vertex (0)[ax] + (((obj->vertex (1)[ax] - obj->vertex (0)[ax]) * (i + 1)) / segments)); }); + + v0.apply ([&](Axis ax, double& a) + { + double len = obj->vertex (1)[ax] - obj->vertex (0)[ax]; + a = (obj->vertex (0)[ax] + ((len * i) / segments)); + }); + + v1.apply ([&](Axis ax, double& a) + { + double len = obj->vertex (1)[ax] - obj->vertex (0)[ax]; + a = (obj->vertex (0)[ax] + ((len * (i + 1)) / segments)); + }); if (obj->type() == OBJ_Line) segment = spawn (v0, v1); diff -r ebc7a186699c -r a496e72af069 src/configDialog.cc --- a/src/configDialog.cc Mon Aug 04 03:35:25 2014 +0300 +++ b/src/configDialog.cc Mon Aug 04 13:43:54 2014 +0300 @@ -245,12 +245,12 @@ void ConfigDialog::_applyToWidgetOptions (std::function func) { // Apply configuration - for (QWidget* wdg : findChildren()) + for (QWidget* widget : findChildren()) { - if (not wdg->objectName().startsWith ("config")) + if (not widget->objectName().startsWith ("config")) continue; - QString confname (wdg->objectName().mid (strlen ("config"))); + QString confname (widget->objectName().mid (strlen ("config"))); AbstractConfigEntry* conf (Config::FindByName (confname)); if (conf == null) @@ -259,7 +259,7 @@ continue; } - func (wdg, conf); + func (widget, conf); } } @@ -268,7 +268,7 @@ // void ConfigDialog::applySettings() { - _applyToWidgetOptions ([&](QWidget* wdg, AbstractConfigEntry* conf) + _applyToWidgetOptions ([&](QWidget* widget, AbstractConfigEntry* conf) { QVariant value (conf->toVariant()); QLineEdit* le; @@ -278,23 +278,22 @@ QCheckBox* checkbox; QPushButton* button; - if ((le = qobject_cast (wdg)) != null) + if ((le = qobject_cast (widget)) != null) value = le->text(); - elif ((spinbox = qobject_cast (wdg)) != null) + elif ((spinbox = qobject_cast (widget)) != null) value = spinbox->value(); - elif ((doublespinbox = qobject_cast (wdg)) != null) + elif ((doublespinbox = qobject_cast (widget)) != null) value = doublespinbox->value(); - elif ((slider = qobject_cast (wdg)) != null) + elif ((slider = qobject_cast (widget)) != null) value = slider->value(); - elif ((checkbox = qobject_cast (wdg)) != null) + elif ((checkbox = qobject_cast (widget)) != null) value = checkbox->isChecked(); - elif ((button = qobject_cast (wdg)) != null) + elif ((button = qobject_cast (widget)) != null) value = _buttonColors[button]; else - print ("Unknown widget of type %1\n", wdg->metaObject()->className()); + print ("Unknown widget of type %1\n", widget->metaObject()->className()); conf->loadFromVariant (value); - print ("Value of %1: %2\n", conf->name(), conf->toVariant().toString()); }); // Rebuild the quick color toolbar @@ -423,17 +422,15 @@ if (not ColorSelector::selectColor (val, defval, this)) return; - if (entry) + if (entry != null) { entry->setColor (val); } else { LDQuickColor entry (val, null); - item = getSelectedQuickColor(); int idx = (item) ? getItemRow (item, quickColorItems) + 1 : quickColorItems.size(); - quickColors.insert (idx, entry); entry = quickColors[idx]; } @@ -471,10 +468,7 @@ if (dest < 0 or dest >= quickColorItems.size()) return; // destination out of bounds - LDQuickColor tmp = quickColors[dest]; - quickColors[dest] = quickColors[idx]; - quickColors[idx] = tmp; - + qSwap (quickColors[dest], quickColors[idx]); updateQuickColorList (&quickColors[dest]); } @@ -510,18 +504,13 @@ return; } - print ("Color of %1 is %2\n", button, _buttonColors[button].name()); - QColor col = QColorDialog::getColor (_buttonColors[button]); + QColor color = QColorDialog::getColor (_buttonColors[button]); - if (col.isValid()) + if (color.isValid()) { - int r = col.red(); - int g = col.green(); - int b = col.blue(); - - QString colname; - colname.sprintf ("#%.2X%.2X%.2X", r, g, b); - setButtonBackground (button, colname); + QString colorname; + colorname.sprintf ("#%.2X%.2X%.2X", color.red(), color.green(), color.blue()); + setButtonBackground (button, colorname); } } @@ -534,7 +523,6 @@ button->setAutoFillBackground (true); button->setStyleSheet (format ("background-color: %1", value)); _buttonColors[button] = QColor (value); - print ("Color of %1 set to %2\n", button, value); } // diff -r ebc7a186699c -r a496e72af069 src/ldObject.h --- a/src/ldObject.h Mon Aug 04 03:35:25 2014 +0300 +++ b/src/ldObject.h Mon Aug 04 13:43:54 2014 +0300 @@ -25,14 +25,16 @@ #define LDOBJ(T) \ public: \ + static constexpr LDObjectType SubclassType = OBJ_##T; \ + LD##T (LDObjectPtr* selfptr); \ + \ virtual LDObjectType type() const override \ { \ return OBJ_##T; \ } \ + \ virtual QString asText() const override; \ virtual void invert() override; \ - \ - LD##T (LDObjectPtr* selfptr); \ #define LDOBJ_NAME(N) public: virtual QString typeName() const override { return #N; } #define LDOBJ_VERTICES(V) public: virtual int numVertices() const override { return V; } @@ -594,3 +596,35 @@ static const int HighResolution = 48; QString PreferredLicenseText(); + +template +inline void DynamicExecute (LDObjectPtr obj, std::function const&)> func) +{ + static_assert (std::is_base_of::value, + "DynamicExecute may only be used with LDObject-derivatives"); + + if (obj->type() == T::SubclassType) + func (obj.staticCast()); +} + +struct LDIterationBreakage {}; + +template +inline void LDIterate (LDObjectList const& objs, + std::function const&)> func) +{ + static_assert (std::is_base_of::value, + "LDIterate may only be used with LDObject-derivatives"); + + try + { + for (LDObjectPtr const& obj : objs) + DynamicExecute (obj, func); + } + catch (LDIterationBreakage) {} +} + +inline void Break() +{ + throw LDIterationBreakage(); +} diff -r ebc7a186699c -r a496e72af069 src/miscallenous.cc --- a/src/miscallenous.cc Mon Aug 04 03:35:25 2014 +0300 +++ b/src/miscallenous.cc Mon Aug 04 13:43:54 2014 +0300 @@ -255,7 +255,7 @@ // void RoundToDecimals (double& a, int decimals) { - assert (decimals >= 0 and decimals < (signed) (sizeof E10 / sizeof *E10)); + assert (decimals >= 0 and decimals < countof (E10)); a = round (a * E10[decimals]) / E10[decimals]; }