[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[freetype2-demos] reorg-widgets a0c7140 2/2: [ftinspect] Re-organize cus
From: |
Werner Lemberg |
Subject: |
[freetype2-demos] reorg-widgets a0c7140 2/2: [ftinspect] Re-organize custom widgets |
Date: |
Mon, 27 Jun 2022 07:49:57 -0400 (EDT) |
branch: reorg-widgets
commit a0c7140f5ac88e3869b7eff003a98160e43ba12c
Author: Charlie Jiang <w@chariri.moe>
Commit: Charlie Jiang <w@chariri.moe>
[ftinspect] Re-organize custom widgets
* src/ftinspect/maingui.hpp: Switch from individual widget files to the new
merged one
* src/ftinspect/widgets/custom_widgets.{cpp, hpp}: All 4 custom widgets
* src/ftinspect/widgets/qcomboboxx.{cpp, hpp}: Merged into custom_widgets
* src/ftinspect/widgets/qgraphicsviewx.{cpp, hpp}: Merged into
custom_widgets
* src/ftinspect/widgets/qpushbottonx.{cpp, hpp}: Merged into custom_widgets
* src/ftinspect/widgets/qspinbox.{cpp, hpp}: Merged into custom_widgets
those widgets could be considered "auxiliary components", and are pretty
far from the core logic. Therefore, they should be made less "loud" (it's 8
source files out of 23 in total - more than 1/3 !).
---
src/ftinspect/CMakeLists.txt | 5 +-
src/ftinspect/maingui.hpp | 5 +-
src/ftinspect/meson.build | 10 +-
src/ftinspect/widgets/custom_widgets.cpp | 186 +++++++++++++++++++++++++++++++
src/ftinspect/widgets/custom_widgets.hpp | 78 +++++++++++++
src/ftinspect/widgets/qcomboboxx.cpp | 37 ------
src/ftinspect/widgets/qcomboboxx.hpp | 24 ----
src/ftinspect/widgets/qgraphicsviewx.cpp | 49 --------
src/ftinspect/widgets/qgraphicsviewx.hpp | 32 ------
src/ftinspect/widgets/qpushbuttonx.cpp | 33 ------
src/ftinspect/widgets/qpushbuttonx.hpp | 24 ----
src/ftinspect/widgets/qspinboxx.cpp | 85 --------------
src/ftinspect/widgets/qspinboxx.hpp | 24 ----
13 files changed, 268 insertions(+), 324 deletions(-)
diff --git a/src/ftinspect/CMakeLists.txt b/src/ftinspect/CMakeLists.txt
index c910e05..0ba066b 100644
--- a/src/ftinspect/CMakeLists.txt
+++ b/src/ftinspect/CMakeLists.txt
@@ -27,10 +27,7 @@ add_executable(ftinspect
"rendering/glyphpoints.cpp"
"rendering/grid.cpp"
- "widgets/qcomboboxx.cpp"
- "widgets/qgraphicsviewx.cpp"
- "widgets/qpushbuttonx.cpp"
- "widgets/qspinboxx.cpp"
+ "widgets/custom_widgets.cpp"
)
target_link_libraries(ftinspect
Qt5::Core Qt5::Widgets
diff --git a/src/ftinspect/maingui.hpp b/src/ftinspect/maingui.hpp
index 2b0b857..46f3798 100644
--- a/src/ftinspect/maingui.hpp
+++ b/src/ftinspect/maingui.hpp
@@ -10,10 +10,7 @@
#include "rendering/glyphoutline.hpp"
#include "rendering/glyphpointnumbers.hpp"
#include "rendering/glyphpoints.hpp"
-#include "widgets/qcomboboxx.hpp"
-#include "widgets/qgraphicsviewx.hpp"
-#include "widgets/qpushbuttonx.hpp"
-#include "widgets/qspinboxx.hpp"
+#include "widgets/custom_widgets.hpp"
#include <QAction>
#include <QCheckBox>
diff --git a/src/ftinspect/meson.build b/src/ftinspect/meson.build
index 9d2abbd..a0689ab 100644
--- a/src/ftinspect/meson.build
+++ b/src/ftinspect/meson.build
@@ -26,20 +26,14 @@ if qt5_dep.found()
'rendering/glyphpointnumbers.cpp',
'rendering/glyphpoints.cpp',
'rendering/grid.cpp',
- 'widgets/qcomboboxx.cpp',
- 'widgets/qgraphicsviewx.cpp',
- 'widgets/qpushbuttonx.cpp',
- 'widgets/qspinboxx.cpp',
+ 'widgets/custom_widgets.cpp',
'ftinspect.cpp',
'maingui.cpp',
])
moc_files = qt5.preprocess(
moc_headers: [
- 'widgets/qcomboboxx.hpp',
- 'widgets/qgraphicsviewx.hpp',
- 'widgets/qpushbuttonx.hpp',
- 'widgets/qspinboxx.hpp',
+ 'widgets/custom_widgets.hpp',
'maingui.hpp',
],
dependencies: qt5_dep)
diff --git a/src/ftinspect/widgets/custom_widgets.cpp
b/src/ftinspect/widgets/custom_widgets.cpp
new file mode 100644
index 0000000..27fbe8f
--- /dev/null
+++ b/src/ftinspect/widgets/custom_widgets.cpp
@@ -0,0 +1,186 @@
+// custom_widgets.cpp
+
+// Copyright (C) 2016-2022 by Werner Lemberg.
+
+#include "custom_widgets.hpp"
+
+#include <QStandardItemModel>
+#include <QScrollBar>
+#include <QStyleOptionButton>
+
+// ----------------------------
+// >>>>>>>> QComboBoxx <<<<<<<<
+// ----------------------------
+
+void
+QComboBoxx::setItemEnabled(int index,
+ bool enable)
+{
+ const QStandardItemModel* itemModel =
+ qobject_cast<const QStandardItemModel*>(model());
+ QStandardItem* item = itemModel->item(index);
+
+ if (enable)
+ {
+ item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
+ item->setData(QVariant(),
+ Qt::TextColorRole);
+ }
+ else
+ {
+ item->setFlags(item->flags()
+ & ~(Qt::ItemIsSelectable | Qt::ItemIsEnabled));
+ // clear item data in order to use default color;
+ // this visually greys out the item
+ item->setData(palette().color(QPalette::Disabled, QPalette::Text),
+ Qt::TextColorRole);
+ }
+}
+
+// --------------------------------
+// >>>>>>>> QGraphicsViewx <<<<<<<<
+// --------------------------------
+
+QGraphicsViewx::QGraphicsViewx()
+: lastBottomLeftPointInitialized(false)
+{
+ // empty
+}
+
+
+void
+QGraphicsViewx::scrollContentsBy(int dx,
+ int dy)
+{
+ QGraphicsView::scrollContentsBy(dx, dy);
+ lastBottomLeftPoint = viewport()->rect().bottomLeft();
+}
+
+
+void
+QGraphicsViewx::resizeEvent(QResizeEvent* event)
+{
+ QGraphicsView::resizeEvent(event);
+
+ // XXX I don't know how to properly initialize this value,
+ // thus the hack with the boolean
+ if (!lastBottomLeftPointInitialized)
+ {
+ lastBottomLeftPoint = viewport()->rect().bottomLeft();
+ lastBottomLeftPointInitialized = true;
+ }
+
+ QPointF currentBottomLeftPoint = viewport()->rect().bottomLeft();
+ int verticalPosition = verticalScrollBar()->value();
+ verticalScrollBar()->setValue(static_cast<int>(
+ verticalPosition
+ - (currentBottomLeftPoint.y()
+ - lastBottomLeftPoint.y())));
+}
+
+// ------------------------------
+// >>>>>>>> QPushButtonx <<<<<<<<
+// ------------------------------
+
+// code derived from Qt 4.8.7, function `QPushButton::sizeHint',
+// file `src/gui/widgets/qpushbutton.cpp'
+
+QPushButtonx::QPushButtonx(const QString &text,
+ QWidget *parent)
+: QPushButton(text, parent)
+{
+ QStyleOptionButton opt;
+ opt.initFrom(this);
+ QString s(this->text());
+ QFontMetrics fm = fontMetrics();
+ QSize sz = fm.size(Qt::TextShowMnemonic, s);
+ opt.rect.setSize(sz);
+
+ sz = style()->sizeFromContents(QStyle::CT_PushButton,
+ &opt,
+ sz,
+ this);
+ setFixedWidth(sz.width());
+}
+
+// ---------------------------
+// >>>>>>>> QSpinBoxx <<<<<<<<
+// ---------------------------
+
+// we want to mark the center of a pixel square with a single dot or a small
+// cross; starting with a certain magnification we thus only use even values
+// so that we can do that symmetrically
+
+int
+QSpinBoxx::valueFromText(const QString& text) const
+{
+ int val = QSpinBox::valueFromText(text);
+
+ if (val > 640)
+ val = val - (val % 64);
+ else if (val > 320)
+ val = val - (val % 32);
+ else if (val > 160)
+ val = val - (val % 16);
+ else if (val > 80)
+ val = val - (val % 8);
+ else if (val > 40)
+ val = val - (val % 4);
+ else if (val > 20)
+ val = val - (val % 2);
+
+ return val;
+}
+
+
+void
+QSpinBoxx::stepBy(int steps)
+{
+ int val = value();
+
+ if (steps > 0)
+ {
+ for (int i = 0; i < steps; i++)
+ {
+ if (val >= 640)
+ val = val + 64;
+ else if (val >= 320)
+ val = val + 32;
+ else if (val >= 160)
+ val = val + 16;
+ else if (val >= 80)
+ val = val + 8;
+ else if (val >= 40)
+ val = val + 4;
+ else if (val >= 20)
+ val = val + 2;
+ else
+ val++;
+ }
+ }
+ else if (steps < 0)
+ {
+ for (int i = 0; i < -steps; i++)
+ {
+ if (val > 640)
+ val = val - 64;
+ else if (val > 320)
+ val = val - 32;
+ else if (val > 160)
+ val = val - 16;
+ else if (val > 80)
+ val = val - 8;
+ else if (val > 40)
+ val = val - 4;
+ else if (val > 20)
+ val = val - 2;
+ else
+ val--;
+ }
+ }
+
+ setValue(val);
+}
+
+
+// end of custom_widgets.cpp
diff --git a/src/ftinspect/widgets/custom_widgets.hpp
b/src/ftinspect/widgets/custom_widgets.hpp
new file mode 100644
index 0000000..dc889e1
--- /dev/null
+++ b/src/ftinspect/widgets/custom_widgets.hpp
@@ -0,0 +1,78 @@
+// custom_widgets.hpp
+
+// Copyright (C) 2016-2022 by Werner Lemberg.
+
+#pragma once
+
+#include <QComboBox>
+#include <QGraphicsView>
+#include <QPushButton>
+#include <QSpinBox>
+#include <QString>
+
+// We need to define a series of custom Qt widgets to satisfy.
+// Basically those custom widgets are derived classes from Qt-provided
components,
+// with minor changes.
+// Because all those derived classes are pretty tiny and not core logic,
they're
+// organized into one single hpp/cpp pair.
+
+// we want to grey out items in a combo box;
+// since Qt doesn't provide a function for this we derive a class
+class QComboBoxx
+: public QComboBox
+{
+ Q_OBJECT
+
+public:
+ void setItemEnabled(int index,
+ bool enable);
+};
+
+
+// we want to anchor the view at the bottom left corner
+// while the windows gets resized
+class QGraphicsViewx
+: public QGraphicsView
+{
+ Q_OBJECT
+
+public:
+ QGraphicsViewx();
+
+protected:
+ void resizeEvent(QResizeEvent* event);
+ void scrollContentsBy(int dx,
+ int dy);
+
+private:
+ QPointF lastBottomLeftPoint;
+ bool lastBottomLeftPointInitialized;
+};
+
+
+// we want buttons that are horizontally as small as possible
+class QPushButtonx
+: public QPushButton
+{
+ Q_OBJECT
+
+public:
+ QPushButtonx(const QString& text,
+ QWidget* = 0);
+ virtual ~QPushButtonx(){}
+};
+
+
+// we want to have our own `stepBy' function for the zoom spin box
+class QSpinBoxx
+: public QSpinBox
+{
+ Q_OBJECT
+
+public:
+ void stepBy(int val);
+ int valueFromText(const QString& text) const;
+};
+
+
+// end of custom_widgets.hpp
diff --git a/src/ftinspect/widgets/qcomboboxx.cpp
b/src/ftinspect/widgets/qcomboboxx.cpp
deleted file mode 100644
index 6b3a792..0000000
--- a/src/ftinspect/widgets/qcomboboxx.cpp
+++ /dev/null
@@ -1,37 +0,0 @@
-// qcomboboxx.cpp
-
-// Copyright (C) 2016-2022 by Werner Lemberg.
-
-
-#include "qcomboboxx.hpp"
-
-#include <QStandardItemModel>
-
-
-void
-QComboBoxx::setItemEnabled(int index,
- bool enable)
-{
- const QStandardItemModel* itemModel =
- qobject_cast<const QStandardItemModel*>(model());
- QStandardItem* item = itemModel->item(index);
-
- if (enable)
- {
- item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
- item->setData(QVariant(),
- Qt::TextColorRole);
- }
- else
- {
- item->setFlags(item->flags()
- & ~(Qt::ItemIsSelectable | Qt::ItemIsEnabled));
- // clear item data in order to use default color;
- // this visually greys out the item
- item->setData(palette().color(QPalette::Disabled, QPalette::Text),
- Qt::TextColorRole);
- }
-}
-
-
-// end of qcomboboxx.cpp
diff --git a/src/ftinspect/widgets/qcomboboxx.hpp
b/src/ftinspect/widgets/qcomboboxx.hpp
deleted file mode 100644
index f660d67..0000000
--- a/src/ftinspect/widgets/qcomboboxx.hpp
+++ /dev/null
@@ -1,24 +0,0 @@
-// qcomboboxx.hpp
-
-// Copyright (C) 2016-2022 by Werner Lemberg.
-
-
-#pragma once
-
-#include <QComboBox>
-
-
-// we want to grey out items in a combo box;
-// since Qt doesn't provide a function for this we derive a class
-class QComboBoxx
-: public QComboBox
-{
- Q_OBJECT
-
-public:
- void setItemEnabled(int index,
- bool enable);
-};
-
-
-// end of qcomboboxx.hpp
diff --git a/src/ftinspect/widgets/qgraphicsviewx.cpp
b/src/ftinspect/widgets/qgraphicsviewx.cpp
deleted file mode 100644
index a4e0ba9..0000000
--- a/src/ftinspect/widgets/qgraphicsviewx.cpp
+++ /dev/null
@@ -1,49 +0,0 @@
-// qgraphicsviewx.hpp
-
-// Copyright (C) 2016-2022 by Werner Lemberg.
-
-
-#include "qgraphicsviewx.hpp"
-
-#include <QScrollBar>
-
-
-QGraphicsViewx::QGraphicsViewx()
-: lastBottomLeftPointInitialized(false)
-{
- // empty
-}
-
-
-void
-QGraphicsViewx::scrollContentsBy(int dx,
- int dy)
-{
- QGraphicsView::scrollContentsBy(dx, dy);
- lastBottomLeftPoint = viewport()->rect().bottomLeft();
-}
-
-
-void
-QGraphicsViewx::resizeEvent(QResizeEvent* event)
-{
- QGraphicsView::resizeEvent(event);
-
- // XXX I don't know how to properly initialize this value,
- // thus the hack with the boolean
- if (!lastBottomLeftPointInitialized)
- {
- lastBottomLeftPoint = viewport()->rect().bottomLeft();
- lastBottomLeftPointInitialized = true;
- }
-
- QPointF currentBottomLeftPoint = viewport()->rect().bottomLeft();
- int verticalPosition = verticalScrollBar()->value();
- verticalScrollBar()->setValue(static_cast<int>(
- verticalPosition
- - (currentBottomLeftPoint.y()
- - lastBottomLeftPoint.y())));
-}
-
-
-// end of qgraphicsviewx.cpp
diff --git a/src/ftinspect/widgets/qgraphicsviewx.hpp
b/src/ftinspect/widgets/qgraphicsviewx.hpp
deleted file mode 100644
index 1a55b2c..0000000
--- a/src/ftinspect/widgets/qgraphicsviewx.hpp
+++ /dev/null
@@ -1,32 +0,0 @@
-// qgraphicsviewx.hpp
-
-// Copyright (C) 2016-2022 by Werner Lemberg.
-
-
-#pragma once
-
-#include <QGraphicsView>
-
-
-// we want to anchor the view at the bottom left corner
-// while the windows gets resized
-class QGraphicsViewx
-: public QGraphicsView
-{
- Q_OBJECT
-
-public:
- QGraphicsViewx();
-
-protected:
- void resizeEvent(QResizeEvent* event);
- void scrollContentsBy(int dx,
- int dy);
-
-private:
- QPointF lastBottomLeftPoint;
- bool lastBottomLeftPointInitialized;
-};
-
-
-// end of qgraphicsviewx.hpp
diff --git a/src/ftinspect/widgets/qpushbuttonx.cpp
b/src/ftinspect/widgets/qpushbuttonx.cpp
deleted file mode 100644
index c92464b..0000000
--- a/src/ftinspect/widgets/qpushbuttonx.cpp
+++ /dev/null
@@ -1,33 +0,0 @@
-// qpushbuttonx.cpp
-
-// Copyright (C) 2016-2022 by Werner Lemberg.
-
-
-#include "qpushbuttonx.hpp"
-
-#include <QStyleOptionButton>
-
-
-// code derived from Qt 4.8.7, function `QPushButton::sizeHint',
-// file `src/gui/widgets/qpushbutton.cpp'
-
-QPushButtonx::QPushButtonx(const QString &text,
- QWidget *parent)
-: QPushButton(text, parent)
-{
- QStyleOptionButton opt;
- opt.initFrom(this);
- QString s(this->text());
- QFontMetrics fm = fontMetrics();
- QSize sz = fm.size(Qt::TextShowMnemonic, s);
- opt.rect.setSize(sz);
-
- sz = style()->sizeFromContents(QStyle::CT_PushButton,
- &opt,
- sz,
- this);
- setFixedWidth(sz.width());
-}
-
-
-// end of qpushbuttonx.cpp
diff --git a/src/ftinspect/widgets/qpushbuttonx.hpp
b/src/ftinspect/widgets/qpushbuttonx.hpp
deleted file mode 100644
index 76c6b79..0000000
--- a/src/ftinspect/widgets/qpushbuttonx.hpp
+++ /dev/null
@@ -1,24 +0,0 @@
-// qpushbuttonx.hpp
-
-// Copyright (C) 2016-2022 by Werner Lemberg.
-
-
-#pragma once
-
-#include <QPushButton>
-
-
-// we want buttons that are horizontally as small as possible
-class QPushButtonx
-: public QPushButton
-{
- Q_OBJECT
-
-public:
- QPushButtonx(const QString& text,
- QWidget* = 0);
- virtual ~QPushButtonx(){}
-};
-
-
-// end of qpushbuttonx.hpp
diff --git a/src/ftinspect/widgets/qspinboxx.cpp
b/src/ftinspect/widgets/qspinboxx.cpp
deleted file mode 100644
index 3d40422..0000000
--- a/src/ftinspect/widgets/qspinboxx.cpp
+++ /dev/null
@@ -1,85 +0,0 @@
-// qspinboxx.cpp
-
-// Copyright (C) 2016-2022 by Werner Lemberg.
-
-
-#include "qspinboxx.hpp"
-
-
-// we want to mark the center of a pixel square with a single dot or a small
-// cross; starting with a certain magnification we thus only use even values
-// so that we can do that symmetrically
-
-int
-QSpinBoxx::valueFromText(const QString& text) const
-{
- int val = QSpinBox::valueFromText(text);
-
- if (val > 640)
- val = val - (val % 64);
- else if (val > 320)
- val = val - (val % 32);
- else if (val > 160)
- val = val - (val % 16);
- else if (val > 80)
- val = val - (val % 8);
- else if (val > 40)
- val = val - (val % 4);
- else if (val > 20)
- val = val - (val % 2);
-
- return val;
-}
-
-
-void
-QSpinBoxx::stepBy(int steps)
-{
- int val = value();
-
- if (steps > 0)
- {
- for (int i = 0; i < steps; i++)
- {
- if (val >= 640)
- val = val + 64;
- else if (val >= 320)
- val = val + 32;
- else if (val >= 160)
- val = val + 16;
- else if (val >= 80)
- val = val + 8;
- else if (val >= 40)
- val = val + 4;
- else if (val >= 20)
- val = val + 2;
- else
- val++;
- }
- }
- else if (steps < 0)
- {
- for (int i = 0; i < -steps; i++)
- {
- if (val > 640)
- val = val - 64;
- else if (val > 320)
- val = val - 32;
- else if (val > 160)
- val = val - 16;
- else if (val > 80)
- val = val - 8;
- else if (val > 40)
- val = val - 4;
- else if (val > 20)
- val = val - 2;
- else
- val--;
- }
- }
-
- setValue(val);
-}
-
-
-// end of qspinboxx.cpp
diff --git a/src/ftinspect/widgets/qspinboxx.hpp
b/src/ftinspect/widgets/qspinboxx.hpp
deleted file mode 100644
index 98a081a..0000000
--- a/src/ftinspect/widgets/qspinboxx.hpp
+++ /dev/null
@@ -1,24 +0,0 @@
-// qspinboxx.hpp
-
-// Copyright (C) 2016-2022 by Werner Lemberg.
-
-
-#pragma once
-
-#include <QSpinBox>
-#include <QString>
-
-
-// we want to have our own `stepBy' function for the zoom spin box
-class QSpinBoxx
-: public QSpinBox
-{
- Q_OBJECT
-
-public:
- void stepBy(int val);
- int valueFromText(const QString& text) const;
-};
-
-
-// qspinboxx.hpp
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [freetype2-demos] reorg-widgets a0c7140 2/2: [ftinspect] Re-organize custom widgets,
Werner Lemberg <=