diff options
Diffstat (limited to 'src/widgets/dialogs')
| -rw-r--r-- | src/widgets/dialogs/qcolordialog.cpp | 256 | ||||
| -rw-r--r-- | src/widgets/dialogs/qcolorwell_p.h | 142 | ||||
| -rw-r--r-- | src/widgets/dialogs/qfiledialog.cpp | 1 | ||||
| -rw-r--r-- | src/widgets/dialogs/qfiledialog.ui | 10 | ||||
| -rw-r--r-- | src/widgets/dialogs/qfiledialog_p.h | 5 | ||||
| -rw-r--r-- | src/widgets/dialogs/qfontdialog.cpp | 2 |
6 files changed, 252 insertions, 164 deletions
diff --git a/src/widgets/dialogs/qcolordialog.cpp b/src/widgets/dialogs/qcolordialog.cpp index b4ca17b9f64..f8125204045 100644 --- a/src/widgets/dialogs/qcolordialog.cpp +++ b/src/widgets/dialogs/qcolordialog.cpp @@ -39,6 +39,7 @@ #include "qwindow.h" #include "private/qdialog_p.h" +#include "private/qcolorwell_p.h" #include <qpa/qplatformintegration.h> #include <qpa/qplatformservices.h> @@ -56,16 +57,12 @@ namespace QtPrivate { class QColorLuminancePicker; class QColorPicker; class QColorShower; -class QWellArray; -class QColorWell; class QColorPickingEventFilter; } // namespace QtPrivate using QColorLuminancePicker = QtPrivate::QColorLuminancePicker; using QColorPicker = QtPrivate::QColorPicker; using QColorShower = QtPrivate::QColorShower; -using QWellArray = QtPrivate::QWellArray; -using QColorWell = QtPrivate::QColorWell; using QColorPickingEventFilter = QtPrivate::QColorPickingEventFilter; class QColorDialogPrivate : public QDialogPrivate @@ -162,96 +159,6 @@ private: //////////// QWellArray BEGIN -namespace QtPrivate { - -class QWellArray : public QWidget -{ - Q_OBJECT - Q_PROPERTY(int selectedColumn READ selectedColumn) - Q_PROPERTY(int selectedRow READ selectedRow) - -public: - QWellArray(int rows, int cols, QWidget* parent=nullptr); - ~QWellArray() {} - QString cellContent(int row, int col) const; - - int selectedColumn() const { return selCol; } - int selectedRow() const { return selRow; } - - virtual void setCurrent(int row, int col); - virtual void setSelected(int row, int col); - - QSize sizeHint() const override; - - inline int cellWidth() const - { return cellw; } - - inline int cellHeight() const - { return cellh; } - - inline int rowAt(int y) const - { return y / cellh; } - - inline int columnAt(int x) const - { if (isRightToLeft()) return ncols - (x / cellw) - 1; return x / cellw; } - - inline int rowY(int row) const - { return cellh * row; } - - inline int columnX(int column) const - { if (isRightToLeft()) return cellw * (ncols - column - 1); return cellw * column; } - - inline int numRows() const - { return nrows; } - - inline int numCols() const - {return ncols; } - - inline QRect cellRect() const - { return QRect(0, 0, cellw, cellh); } - - inline QSize gridSize() const - { return QSize(ncols * cellw, nrows * cellh); } - - QRect cellGeometry(int row, int column) - { - QRect r; - if (row >= 0 && row < nrows && column >= 0 && column < ncols) - r.setRect(columnX(column), rowY(row), cellw, cellh); - return r; - } - - inline void updateCell(int row, int column) { update(cellGeometry(row, column)); } - -signals: - void selected(int row, int col); - void currentChanged(int row, int col); - void colorChanged(int index, QRgb color); - -protected: - virtual void paintCell(QPainter *, int row, int col, const QRect&); - virtual void paintCellContents(QPainter *, int row, int col, const QRect&); - - void mousePressEvent(QMouseEvent*) override; - void mouseReleaseEvent(QMouseEvent*) override; - void keyPressEvent(QKeyEvent*) override; - void focusInEvent(QFocusEvent*) override; - void focusOutEvent(QFocusEvent*) override; - void paintEvent(QPaintEvent *) override; - -private: - Q_DISABLE_COPY(QWellArray) - - int nrows; - int ncols; - int cellw; - int cellh; - int curRow; - int curCol; - int selRow; - int selCol; -}; - void QWellArray::paintEvent(QPaintEvent *e) { QRect r = e->rect(); @@ -476,11 +383,12 @@ void QWellArray::keyPressEvent(QKeyEvent* e) e->ignore(); // we don't accept the event return; } - -} // namespace QtPrivate +} //////////// QWellArray END +namespace QtPrivate { + // Event filter to be installed on the dialog while in color-picking mode. class QColorPickingEventFilter : public QObject { public: @@ -511,7 +419,7 @@ private: QColorDialogPrivate *m_dp; }; -} // unnamed namespace +} // namespace QtPrivate /*! Returns the number of custom colors supported by QColorDialog. All @@ -571,35 +479,6 @@ static inline void rgb2hsv(QRgb rgb, int &h, int &s, int &v) c.getHsv(&h, &s, &v); } -namespace QtPrivate { - -class QColorWell : public QWellArray -{ -public: - QColorWell(QWidget *parent, int r, int c, const QRgb *vals) - :QWellArray(r, c, parent), values(vals), mousePressed(false), oldCurrent(-1, -1) - { setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum)); } - -protected: - void paintCellContents(QPainter *, int row, int col, const QRect&) override; - void mousePressEvent(QMouseEvent *e) override; - void mouseMoveEvent(QMouseEvent *e) override; - void mouseReleaseEvent(QMouseEvent *e) override; -#if QT_CONFIG(draganddrop) - void dragEnterEvent(QDragEnterEvent *e) override; - void dragLeaveEvent(QDragLeaveEvent *e) override; - void dragMoveEvent(QDragMoveEvent *e) override; - void dropEvent(QDropEvent *e) override; -#endif - -private: - const QRgb *values; - bool mousePressed; - QPoint pressPos; - QPoint oldCurrent; - -}; - void QColorWell::paintCellContents(QPainter *p, int row, int col, const QRect &r) { int i = row + col*numRows(); @@ -687,6 +566,8 @@ void QColorWell::mouseReleaseEvent(QMouseEvent *e) mousePressed = false; } +namespace QtPrivate { + class QColorPicker : public QFrame { Q_OBJECT @@ -704,18 +585,21 @@ signals: protected: QSize sizeHint() const override; void paintEvent(QPaintEvent*) override; + void keyPressEvent(QKeyEvent *event) override; void mouseMoveEvent(QMouseEvent *) override; void mousePressEvent(QMouseEvent *) override; void resizeEvent(QResizeEvent *) override; private: - int hue; - int sat; + QPoint m_pos; - QPoint colPt(); - int huePt(const QPoint &pt); - int satPt(const QPoint &pt); - void setCol(const QPoint &pt); + QPixmap createColorsPixmap(); + QPoint colPt(int hue, int sat); + int huePt(const QPoint &pt, const QSize &widgetSize); + int huePt(const QPoint &pt) { return huePt(pt, size()); } + int satPt(const QPoint &pt, const QSize &widgetSize); + int satPt(const QPoint &pt) { return satPt(pt, size()); } + void setCol(const QPoint &pt, bool notify = true); QPixmap pix; bool crossVisible; @@ -744,6 +628,7 @@ signals: protected: void paintEvent(QPaintEvent*) override; + void keyPressEvent(QKeyEvent *event) override; void mouseMoveEvent(QMouseEvent *) override; void mousePressEvent(QMouseEvent *) override; @@ -779,6 +664,7 @@ QColorLuminancePicker::QColorLuminancePicker(QWidget* parent) hue = 100; val = 100; sat = 100; pix = nullptr; // setAttribute(WA_NoErase, true); + setFocusPolicy(Qt::StrongFocus); } QColorLuminancePicker::~QColorLuminancePicker() @@ -786,6 +672,21 @@ QColorLuminancePicker::~QColorLuminancePicker() delete pix; } +void QColorLuminancePicker::keyPressEvent(QKeyEvent *event) +{ + switch (event->key()) { + case Qt::Key_Down: + setVal(std::clamp(val - 1, 0, 255)); + break; + case Qt::Key_Up: + setVal(std::clamp(val + 1, 0, 255)); + break; + default: + QWidget::keyPressEvent(event); + break; + } +} + void QColorLuminancePicker::mouseMoveEvent(QMouseEvent *m) { if (m->buttons() == Qt::NoButton) { @@ -856,38 +757,53 @@ void QColorLuminancePicker::setCol(int h, int s , int v) repaint(); } -QPoint QColorPicker::colPt() +QPoint QColorPicker::colPt(int hue, int sat) { QRect r = contentsRect(); return QPoint((360 - hue) * (r.width() - 1) / 360, (255 - sat) * (r.height() - 1) / 255); } -int QColorPicker::huePt(const QPoint &pt) +int QColorPicker::huePt(const QPoint &pt, const QSize &widgetSize) { - QRect r = contentsRect(); - return 360 - pt.x() * 360 / (r.width() - 1); + QRect r = QRect(QPoint(0, 0), widgetSize) - contentsMargins(); + return std::clamp(360 - pt.x() * 360 / (r.width() - 1), 0, 359); } -int QColorPicker::satPt(const QPoint &pt) +int QColorPicker::satPt(const QPoint &pt, const QSize &widgetSize) { - QRect r = contentsRect(); - return 255 - pt.y() * 255 / (r.height() - 1); + QRect r = QRect(QPoint(0, 0), widgetSize) - contentsMargins(); + return std::clamp(255 - pt.y() * 255 / (r.height() - 1), 0, 255); } -void QColorPicker::setCol(const QPoint &pt) +void QColorPicker::setCol(const QPoint &pt, bool notify) { - setCol(huePt(pt), satPt(pt)); + if (pt == m_pos) + return; + + QRect r(m_pos, QSize(20, 20)); + m_pos.setX(std::clamp(pt.x(), 0, pix.width() - 1)); + m_pos.setY(std::clamp(pt.y(), 0, pix.height() - 1)); + r = r.united(QRect(m_pos, QSize(20, 20))); + r.translate(contentsRect().x() - 9, contentsRect().y() - 9); + // update(r); + repaint(r); + + if (notify) + emit newCol(huePt(m_pos), satPt(m_pos)); } QColorPicker::QColorPicker(QWidget* parent) : QFrame(parent) , crossVisible(true) { - hue = 0; sat = 0; - setCol(150, 255); - setAttribute(Qt::WA_NoSystemBackground); + setFocusPolicy(Qt::StrongFocus); setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed) ); + adjustSize(); + + pix = createColorsPixmap(); + + setCol(150, 255); } QColorPicker::~QColorPicker() @@ -911,15 +827,31 @@ void QColorPicker::setCol(int h, int s) { int nhue = qMin(qMax(0,h), 359); int nsat = qMin(qMax(0,s), 255); - if (nhue == hue && nsat == sat) + if (nhue == huePt(m_pos) && nsat == satPt(m_pos)) return; - QRect r(colPt(), QSize(20,20)); - hue = nhue; sat = nsat; - r = r.united(QRect(colPt(), QSize(20,20))); - r.translate(contentsRect().x()-9, contentsRect().y()-9); - // update(r); - repaint(r); + setCol(colPt(nhue, nsat), false); +} + +void QColorPicker::keyPressEvent(QKeyEvent *event) +{ + switch (event->key()) { + case Qt::Key_Down: + setCol(m_pos + QPoint(0, 1)); + break; + case Qt::Key_Left: + setCol(m_pos + QPoint(-1, 0)); + break; + case Qt::Key_Right: + setCol(m_pos + QPoint(1, 0)); + break; + case Qt::Key_Up: + setCol(m_pos + QPoint(0, -1)); + break; + default: + QFrame::keyPressEvent(event); + break; + } } void QColorPicker::mouseMoveEvent(QMouseEvent *m) @@ -930,14 +862,12 @@ void QColorPicker::mouseMoveEvent(QMouseEvent *m) return; } setCol(p); - emit newCol(hue, sat); } void QColorPicker::mousePressEvent(QMouseEvent *m) { QPoint p = m->position().toPoint() - contentsRect().topLeft(); setCol(p); - emit newCol(hue, sat); } void QColorPicker::paintEvent(QPaintEvent* ) @@ -949,7 +879,7 @@ void QColorPicker::paintEvent(QPaintEvent* ) p.drawPixmap(r.topLeft(), pix); if (crossVisible) { - QPoint pt = colPt() + r.topLeft(); + QPoint pt = m_pos + r.topLeft(); p.setPen(Qt::black); p.fillRect(pt.x()-9, pt.y(), 20, 2, Qt::black); p.fillRect(pt.x(), pt.y()-9, 2, 20, Qt::black); @@ -960,6 +890,21 @@ void QColorPicker::resizeEvent(QResizeEvent *ev) { QFrame::resizeEvent(ev); + pix = createColorsPixmap(); + + const QSize &oldSize = ev->oldSize(); + if (!oldSize.isValid()) + return; + + // calculate hue/saturation based on previous widget size + // and update position accordingly + const int hue = huePt(m_pos, oldSize); + const int sat = satPt(m_pos, oldSize); + setCol(hue, sat); +} + +QPixmap QColorPicker::createColorsPixmap() +{ int w = width() - frameWidth() * 2; int h = height() - frameWidth() * 2; QImage img(w, h, QImage::Format_RGB32); @@ -977,10 +922,9 @@ void QColorPicker::resizeEvent(QResizeEvent *ev) ++x; } } - pix = QPixmap::fromImage(img); + return QPixmap::fromImage(img); } - class QColSpinBox : public QSpinBox { public: diff --git a/src/widgets/dialogs/qcolorwell_p.h b/src/widgets/dialogs/qcolorwell_p.h new file mode 100644 index 00000000000..31d69fabb13 --- /dev/null +++ b/src/widgets/dialogs/qcolorwell_p.h @@ -0,0 +1,142 @@ +// Copyright (C) 2025 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef QCOLORWELL_P_H +#define QCOLORWELL_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtCore/qrect.h> +#include <QtWidgets/qwidget.h> + +QT_REQUIRE_CONFIG(colordialog); + +QT_BEGIN_NAMESPACE + +class QWellArray : public QWidget +{ + Q_OBJECT + Q_PROPERTY(int selectedColumn READ selectedColumn) + Q_PROPERTY(int selectedRow READ selectedRow) + +public: + QWellArray(int rows, int cols, QWidget *parent = nullptr); + ~QWellArray() { } + + int selectedColumn() const { return selCol; } + int selectedRow() const { return selRow; } + + virtual void setCurrent(int row, int col); + virtual void setSelected(int row, int col); + + QSize sizeHint() const override; + + inline int cellWidth() const { return cellw; } + + inline int cellHeight() const { return cellh; } + + inline int rowAt(int y) const { return y / cellh; } + + inline int columnAt(int x) const + { + if (isRightToLeft()) + return ncols - (x / cellw) - 1; + return x / cellw; + } + + inline int rowY(int row) const { return cellh * row; } + + inline int columnX(int column) const + { + if (isRightToLeft()) + return cellw * (ncols - column - 1); + return cellw * column; + } + + inline int numRows() const { return nrows; } + + inline int numCols() const { return ncols; } + + inline QRect cellRect() const { return QRect(0, 0, cellw, cellh); } + + inline QSize gridSize() const { return QSize(ncols * cellw, nrows * cellh); } + + QRect cellGeometry(int row, int column) + { + QRect r; + if (row >= 0 && row < nrows && column >= 0 && column < ncols) + r.setRect(columnX(column), rowY(row), cellw, cellh); + return r; + } + + inline void updateCell(int row, int column) { update(cellGeometry(row, column)); } + +signals: + void selected(int row, int col); + void currentChanged(int row, int col); + void colorChanged(int index, QRgb color); + +protected: + virtual void paintCell(QPainter *, int row, int col, const QRect &); + virtual void paintCellContents(QPainter *, int row, int col, const QRect &); + + void mousePressEvent(QMouseEvent *) override; + void mouseReleaseEvent(QMouseEvent *) override; + void keyPressEvent(QKeyEvent *) override; + void focusInEvent(QFocusEvent *) override; + void focusOutEvent(QFocusEvent *) override; + void paintEvent(QPaintEvent *) override; + +private: + Q_DISABLE_COPY(QWellArray) + + int nrows; + int ncols; + int cellw; + int cellh; + int curRow; + int curCol; + int selRow; + int selCol; +}; + +class QColorWell : public QWellArray +{ +public: + QColorWell(QWidget *parent, int r, int c, const QRgb *vals) + : QWellArray(r, c, parent), values(vals), mousePressed(false), oldCurrent(-1, -1) + { + setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum)); + } + +protected: + void paintCellContents(QPainter *, int row, int col, const QRect &) override; + void mousePressEvent(QMouseEvent *e) override; + void mouseMoveEvent(QMouseEvent *e) override; + void mouseReleaseEvent(QMouseEvent *e) override; +#if QT_CONFIG(draganddrop) + void dragEnterEvent(QDragEnterEvent *e) override; + void dragLeaveEvent(QDragLeaveEvent *e) override; + void dragMoveEvent(QDragMoveEvent *e) override; + void dropEvent(QDropEvent *e) override; +#endif + +private: + const QRgb *values; + bool mousePressed; + QPoint pressPos; + QPoint oldCurrent; +}; + +QT_END_NAMESPACE + +#endif // QCOLORWELL_P_H diff --git a/src/widgets/dialogs/qfiledialog.cpp b/src/widgets/dialogs/qfiledialog.cpp index d964c9667e6..1ed9dd06e3c 100644 --- a/src/widgets/dialogs/qfiledialog.cpp +++ b/src/widgets/dialogs/qfiledialog.cpp @@ -3008,7 +3008,6 @@ void QFileDialogPrivate::createWidgets() qFileDialogUi->lookInCombo->setDuplicatesEnabled(false); // filename - qFileDialogUi->fileNameEdit->setFileDialogPrivate(this); #ifndef QT_NO_SHORTCUT qFileDialogUi->fileNameLabel->setBuddy(qFileDialogUi->fileNameEdit); #endif diff --git a/src/widgets/dialogs/qfiledialog.ui b/src/widgets/dialogs/qfiledialog.ui index f275e20c633..97f39fa6194 100644 --- a/src/widgets/dialogs/qfiledialog.ui +++ b/src/widgets/dialogs/qfiledialog.ui @@ -20,7 +20,10 @@ <item row="0" column="0"> <widget class="QLabel" name="lookInLabel"> <property name="text"> - <string>Look in:</string> + <string>&Look in:</string> + </property> + <property name="buddy"> + <cstring>lookInCombo</cstring> </property> </widget> </item> @@ -284,7 +287,10 @@ </sizepolicy> </property> <property name="text"> - <string>Files of type:</string> + <string>Files of &type:</string> + </property> + <property name="buddy"> + <cstring>fileTypeCombo</cstring> </property> </widget> </item> diff --git a/src/widgets/dialogs/qfiledialog_p.h b/src/widgets/dialogs/qfiledialog_p.h index 15044cfd291..dd4a2505def 100644 --- a/src/widgets/dialogs/qfiledialog_p.h +++ b/src/widgets/dialogs/qfiledialog_p.h @@ -266,12 +266,9 @@ private: class QFileDialogLineEdit : public QLineEdit { public: - QFileDialogLineEdit(QWidget *parent = nullptr) : QLineEdit(parent), d_ptr(nullptr){} - void setFileDialogPrivate(QFileDialogPrivate *d_pointer) {d_ptr = d_pointer; } + QFileDialogLineEdit(QWidget *parent = nullptr) : QLineEdit(parent) {} void keyPressEvent(QKeyEvent *e) override; bool hideOnEsc; -private: - QFileDialogPrivate *d_ptr; }; class QFileDialogComboBox : public QComboBox diff --git a/src/widgets/dialogs/qfontdialog.cpp b/src/widgets/dialogs/qfontdialog.cpp index 870b0c40faf..c4f8af1a639 100644 --- a/src/widgets/dialogs/qfontdialog.cpp +++ b/src/widgets/dialogs/qfontdialog.cpp @@ -172,7 +172,7 @@ void QFontDialogPrivate::init() sizeAccel = new QLabel(q); #ifndef QT_NO_SHORTCUT - sizeAccel->setBuddy(sizeEdit); + sizeAccel->setBuddy(sizeList); #endif sizeAccel->setIndent(2); |
