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,4 +1,23 @@
#include "shortcuts.h"
#include <QPainter>
#include <QAction>
#include <QKeyEvent>
#include <QHeaderView>
#include <QMenu>
#include <QScrollBar>
#include <QShortcut>
#include <QWidgetAction>
#include <QMainWindow>
#include <QToolBar>
/// ShortcutEdit
ShortcutEdit::ShortcutEdit(QWidget *parent) : CLineEdit(parent) {
ti = nullptr;
ca = nullptr;
connect(this, &CLineEdit::textChanged, this, &ShortcutEdit::textChangedSlot);
}
void ShortcutEdit::keyPressEvent(QKeyEvent * e) {
@@ -6,14 +25,56 @@ void ShortcutEdit::keyPressEvent(QKeyEvent * e) {
km &= ~Qt::KeypadModifier;
km &= ~Qt::GroupSwitchModifier;
if (e->key() != Qt::Key_Control && e->key() != Qt::Key_Shift &&
e->key() != Qt::Key_Alt && e->key() != Qt::Key_Meta)
setText(QKeySequence(km | e->key()).toString());
e->key() != Qt::Key_Alt && e->key() != Qt::Key_Meta) {
setText(QKeySequence(km | e->key()).toString());
}
}
void ShortcutEdit::assignAction(QAction *a) {
clear();
ca = a;
reset();
}
QAction *ShortcutEdit::action() const {
return ca;
}
bool ShortcutEdit::isEmpty() const {
return text().isEmpty();
}
void ShortcutEdit::commit() {
if (!ca) {
return;
}
ca->setShortcut(QKeySequence(text()));
}
void ShortcutEdit::reset() {
if (!ca) {
return;
}
setText(ca->shortcut().toString());
}
void ShortcutEdit::textChangedSlot(QString t) {
if (ti) {
ti->setText(1, t);
}
}
/// Shortcuts
Shortcuts::Shortcuts(QWidget * parent, bool on): QTreeWidget(parent) {
aw = 0;
aw = nullptr;
QImage ti(QSize(16, 16), QImage::Format_ARGB32_Premultiplied);
QPainter p(&ti);
p.setCompositionMode(QPainter::CompositionMode_Clear);
@@ -42,8 +103,9 @@ Shortcuts::Shortcuts(QWidget * parent, bool on): QTreeWidget(parent) {
Shortcuts::~Shortcuts() {
foreach (ShortcutEdit * i, edits)
for (ShortcutEdit * i : edits) {
delete i;
}
edits.clear();
}
@@ -60,9 +122,12 @@ void Shortcuts::changeEvent(QEvent * e) {
void Shortcuts::assignWindow(QWidget * w) {
if (w == 0) return;
while ((qobject_cast<QMainWindow * >(w) == 0) && (w->parentWidget() != 0))
if (!w) {
return;
}
while (!(qobject_cast<QMainWindow * >(w)) && (w->parentWidget())) {
w = w->parentWidget();
}
aw = qobject_cast<QMainWindow * >(w);
updateShortcuts();
}
@@ -71,30 +136,38 @@ void Shortcuts::assignWindow(QWidget * w) {
QStringList Shortcuts::actionTree(QAction * a) {
QStringList tree;
QList<QWidget * > aw = a->associatedWidgets();
if (aw.size() == 0) return tree;
QWidget * cw = 0;
QMenu * tm;
QToolBar * tt;
foreach (QWidget * i, aw) {
tm = qobject_cast<QMenu * >(i);
if (tm == 0) continue;
cw = i;
while (cw != 0) {
tm = qobject_cast<QMenu * >(cw);
if (tm != 0) {
if (!tm->title().isEmpty())
tree.push_front(tm->title());
cw = cw->parentWidget();
} else break;
}
if (!tree.isEmpty()) return tree;
if (aw.size() == 0) {
return tree;
}
foreach (QWidget * i, aw) {
tt = qobject_cast<QToolBar * >(i);
if (tt == 0) continue;
cw = i;
if (!tt->windowTitle().isEmpty())
for (QWidget * i : aw) {
auto tm = qobject_cast<QMenu * >(i);
if (!tm) {
continue;
}
auto cw = i;
while (cw) {
tm = qobject_cast<QMenu * >(cw);
if (tm) {
if (!tm->title().isEmpty()) {
tree.push_front(tm->title());
}
cw = cw->parentWidget();
} else {
break;
}
}
if (!tree.isEmpty()) {
return tree;
}
}
for (QWidget * i : aw) {
auto tt = qobject_cast<QToolBar * >(i);
if (!tt) {
continue;
}
if (!tt->windowTitle().isEmpty()) {
tree.push_front(tt->windowTitle());
}
break;
}
return tree;
@@ -103,8 +176,10 @@ QStringList Shortcuts::actionTree(QAction * a) {
QList<QPair<QString, QKeySequence> > Shortcuts::shortcuts() {
QList<QPair<QString, QKeySequence> > l;
foreach (ShortcutEdit * i, edits) {
if (i->action()->objectName().isEmpty()) continue;
for (const ShortcutEdit * i : edits) {
if (i->action()->objectName().isEmpty()) {
continue;
}
l << QPair<QString, QKeySequence>(i->action()->objectName(), i->text());
}
return l;
@@ -124,19 +199,26 @@ void Shortcuts::clear() {
bool Shortcuts::checkAction(QAction * a) {
if (a->menu() != 0) return false;
if (a->isSeparator()) return false;
if (a->text().isEmpty()) return false;
if (a->associatedWidgets().isEmpty()) return false;
if (QString(a->metaObject()->className()) != "QAction") return false;
if (qobject_cast<QWidgetAction * >(a) != 0) return false;
if (a->menu() || a->isSeparator() || a->text().isEmpty()) {
return false;
}
if (a->associatedWidgets().isEmpty()) {
return false;
}
if (QString(a->metaObject()->className()) != "QAction") {
return false;
}
if (qobject_cast<QWidgetAction * >(a)) {
return false;
}
return true;
}
void Shortcuts::updateShortcuts() {
//return;
if (aw == 0 || !active) return;
if (!aw || !active) {
return;
}
hide();
int cpos = verticalScrollBar()->value();
clear();
@@ -150,12 +232,12 @@ void Shortcuts::updateShortcuts() {
QStringList tree;
bool s = isSortingEnabled(), isFound;
setSortingEnabled(false);
foreach (QAction * i, al) {
for (QAction * i : al) {
if (!checkAction(i)) continue;
edits.push_back(new ShortcutEdit());
tree = actionTree(i);
pi = invisibleRootItem();
foreach (QString t, tree) {
for (const QString & t : tree) {
isFound = false;
for (int j = 0; j < pi->childCount(); ++j) {
if (pi->child(j)->text(0) == t) {
@@ -198,38 +280,55 @@ void Shortcuts::updateShortcuts() {
void Shortcuts::commit() {
foreach (ShortcutEdit * i, edits)
for (ShortcutEdit * i : edits) {
i->commit();
}
}
void Shortcuts::reset() {
foreach (ShortcutEdit * i, edits)
void Shortcuts::resetShortcuts() {
for (ShortcutEdit * i : edits) {
i->reset();
}
updateShortcuts();
}
void Shortcuts::filter(const QString & what) {
hide();
for (int i = 0; i < topLevelItemCount(); ++i)
for (int i = 0; i < topLevelItemCount(); ++i) {
filterTree(topLevelItem(i), what);
}
show();
}
bool Shortcuts::filterTree(QTreeWidgetItem * ti, QString f) {
if (f.isEmpty()) {
for (int i = 0; i < ti->childCount(); ++i)
for (int i = 0; i < ti->childCount(); ++i) {
filterTree(ti->child(i), f);
}
ti->setHidden(false);
return true;
}
bool isFound = false;
for (int i = 0; i < ti->childCount(); ++i)
if (filterTree(ti->child(i), f)) isFound = true;
for (int i = 0; i < ti->childCount(); ++i) {
if (filterTree(ti->child(i), f)) {
isFound = true;
}
}
if (ti->text(0).indexOf(f, 0, Qt::CaseInsensitive) >= 0 ||
ti->text(1).indexOf(f, 0, Qt::CaseInsensitive) >= 0) isFound = true;
ti->text(1).indexOf(f, 0, Qt::CaseInsensitive) >= 0) {
isFound = true;
}
ti->setHidden(!isFound);
return isFound;
}
void Shortcuts::updateEditorGeometries() {
for (ShortcutEdit * i : edits) {
i->setGeometry(visualRect(indexFromItem(i->ti, 1)));
}
}