refactoring qad widgets part 1

c++ cast, nullptr, forward declaration, agregate ui, connect to member functions, order and clear includes
This commit is contained in:
2022-12-11 16:27:04 +03:00
parent 5d9381dd37
commit 728c132f2b
22 changed files with 561 additions and 404 deletions

View File

@@ -1,9 +1,16 @@
#include "colorbutton.h"
#include <QDebug>
#include "qclipboard.h"
#include "qcolor.h"
#include "qmenu.h"
#include <QColorDialog>
#include <QMouseEvent>
#include <QDrag>
#include <QMimeData>
#include <QFrame>
#include <QApplication>
ColorButton::ColorButton(QWidget * parent): QPushButton(parent) {
ColorButton::ColorButton(QWidget * parent): QPushButton(parent), menu(new QMenu(this)) {
frame = false;
options = QColorDialog::ShowAlphaChannel;
back = new QWidget(this);
@@ -17,10 +24,10 @@ ColorButton::ColorButton(QWidget * parent): QPushButton(parent) {
label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
label->show();
pal = label->palette();
a_copy = menu.addAction(QIcon(":/icons/edit-copy.png"), tr("Copy"), this, SLOT(copy()));
a_paste = menu.addAction(QIcon(":/icons/edit-paste.png"), tr("Paste"), this, SLOT(paste()));
menu.addSeparator();
a_mix = menu.addAction(tr("Mix with clipboard"), this, SLOT(mix()));
a_copy = menu->addAction(QIcon(":/icons/edit-copy.png"), tr("Copy"), this, SLOT(copy()));
a_paste = menu->addAction(QIcon(":/icons/edit-paste.png"), tr("Paste"), this, SLOT(paste()));
menu->addSeparator();
a_mix = menu->addAction(tr("Mix with clipboard"), this, SLOT(mix()));
setAcceptDrops(true);
connect(this, SIGNAL(clicked(bool)), this, SLOT(clicked()));
}
@@ -32,8 +39,11 @@ ColorButton::~ColorButton() {
void ColorButton::resizeEvent(QResizeEvent * ) {
if (frame) back->setGeometry(rect());
else back->setGeometry(8, 5, width() - 16, height() - 12);
if (frame) {
back->setGeometry(rect());
} else {
back->setGeometry(8, 5, width() - 16, height() - 12);
}
label->setGeometry(back->geometry());
}
@@ -41,11 +51,11 @@ void ColorButton::resizeEvent(QResizeEvent * ) {
void ColorButton::mousePressEvent(QMouseEvent * e) {
pp = e->pos();
if (e->buttons().testFlag(Qt::RightButton)) {
menu.popup(
menu->popup(
#if QT_VERSION_MAJOR <= 5
((QMouseEvent*)e)->globalPos()
static_cast<QMouseEvent*>(e)->globalPos()
#else
((QMouseEvent*)e)->globalPosition().toPoint()
static_cast<QMouseEvent*>(e)->globalPosition().toPoint()
#endif
);
return;
@@ -107,15 +117,19 @@ void ColorButton::changeEvent(QEvent * e) {
void ColorButton::clicked() {
QColor ret = QColorDialog::getColor(color(), nullptr, tr("Choose color"), options);
if (!ret.isValid()) return;
QColor ret = QColorDialog::getColor(color(), nullptr, tr("Choose color"), static_cast<QColorDialog::ColorDialogOptions>(options));
if (!ret.isValid()) {
return;
}
setColor(ret);
}
void ColorButton::mix() {
QColor c(QApplication::clipboard()->text());
if (!c.isValid()) return;
if (!c.isValid()) {
return;
}
QColor sc = color();
setColor(QColor((c.red() + sc.red()) / 2, (c.green() + sc.green()) / 2, (c.blue() + sc.blue()) / 2, (c.alpha() + sc.alpha()) / 2));
}
@@ -123,10 +137,65 @@ void ColorButton::mix() {
void ColorButton::setColor(const QColor & col) {
if (pal.color(label->backgroundRole()) == col) return;
if (options.testFlag(QColorDialog::ShowAlphaChannel))
if (options & QColorDialog::ShowAlphaChannel) {
pal.setColor(label->backgroundRole(), col);
else
} else {
pal.setColor(label->backgroundRole(), QColor(col.red(), col.green(), col.blue()));
}
label->setPalette(pal);
emit colorChanged(color());
}
void ColorButton::setFrameOnly(bool yes) {
frame = yes;
setFlat(frame);
resizeEvent(nullptr);
}
QColor ColorButton::color() const {
return pal.color(label->backgroundRole());
}
bool ColorButton::useNativeDialog() const {
return !(options & QColorDialog::DontUseNativeDialog);
}
bool ColorButton::useAlphaChannel() const {
return (options & QColorDialog::ShowAlphaChannel);
}
void ColorButton::setUseNativeDialog(bool yes) {
if (yes) {
options &= ~QColorDialog::DontUseNativeDialog;
} else {
options |= QColorDialog::DontUseNativeDialog;
}
}
void ColorButton::setUseAlphaChannel(bool yes) {
if (yes) {
options |= QColorDialog::ShowAlphaChannel;
} else {
options &= ~QColorDialog::ShowAlphaChannel;
}
}
void ColorButton::paste() {
QColor c(QApplication::clipboard()->text());
if (c.isValid()) {
setColor(c);
}
}
void ColorButton::copy() {
QApplication::clipboard()->setText(color().name());
}