From dc6b82b4dc9f3c2e0780ac76382b09579ac14bef Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Wed, 11 Jul 2018 13:18:50 -0500 Subject: [PATCH] Disable combobox scroll hijacking, and only show one event by default (rather than all) --- editor.cpp | 78 ++++++++++++++++++---------------------- editor.h | 5 ++- graphicsview.cpp | 6 ---- mainwindow.cpp | 13 +++++-- mainwindow.ui | 15 ++++++-- noscrollcombobox.cpp | 15 ++++++++ noscrollcombobox.h | 17 +++++++++ noscrollspinbox.cpp | 16 +++++++++ noscrollspinbox.h | 17 +++++++++ objectpropertiesframe.ui | 32 ++++++++++++++--- pretmap.pro | 8 +++-- 11 files changed, 158 insertions(+), 64 deletions(-) create mode 100644 noscrollcombobox.cpp create mode 100644 noscrollcombobox.h create mode 100644 noscrollspinbox.cpp create mode 100644 noscrollspinbox.h diff --git a/editor.cpp b/editor.cpp index 4a56cdb3..2e935b11 100755 --- a/editor.cpp +++ b/editor.cpp @@ -4,6 +4,8 @@ #include #include +bool selectingEvent = false; + Editor::Editor(Ui::MainWindow* ui) { this->ui = ui; @@ -469,6 +471,8 @@ void Editor::displayMapEvents() { delete events_group; } + selected_events->clear(); + events_group = new EventGroup; scene->addItem(events_group); @@ -484,8 +488,7 @@ void Editor::displayMapEvents() { } DraggablePixmapItem *Editor::addMapEvent(Event *event) { - DraggablePixmapItem *object = new DraggablePixmapItem(event); - object->editor = this; + DraggablePixmapItem *object = new DraggablePixmapItem(event, this); events_group->addToGroup(object); return object; } @@ -808,16 +811,22 @@ void Editor::updateDiveEmergeMap(QString mapName, QString direction) { void Editor::updatePrimaryTileset(QString tilesetLabel) { - map->layout->tileset_primary_label = tilesetLabel; - map->layout->tileset_primary = project->getTileset(tilesetLabel); - emit tilesetChanged(map->name); + if (map->layout->tileset_primary_label != tilesetLabel) + { + map->layout->tileset_primary_label = tilesetLabel; + map->layout->tileset_primary = project->getTileset(tilesetLabel); + emit tilesetChanged(map->name); + } } void Editor::updateSecondaryTileset(QString tilesetLabel) { - map->layout->tileset_secondary_label = tilesetLabel; - map->layout->tileset_secondary = project->getTileset(tilesetLabel); - emit tilesetChanged(map->name); + if (map->layout->tileset_secondary_label != tilesetLabel) + { + map->layout->tileset_secondary_label = tilesetLabel; + map->layout->tileset_secondary = project->getTileset(tilesetLabel); + emit tilesetChanged(map->name); + } } void MetatilesPixmapItem::paintTileChanged(Map *map) { @@ -1471,9 +1480,11 @@ void CollisionPixmapItem::pick(QGraphicsSceneMouseEvent *event) { void DraggablePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *mouse) { active = true; - clicking = true; last_x = (mouse->pos().x() + this->pos().x()) / 16; last_y = (mouse->pos().y() + this->pos().y()) / 16; + this->editor->selectMapEvent(this, mouse->modifiers() & Qt::ControlModifier); + this->editor->updateSelectedEvents(); + selectingEvent = true; //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("y")); } @@ -1489,7 +1500,6 @@ void DraggablePixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouse) { int x = (mouse->pos().x() + this->pos().x()) / 16; int y = (mouse->pos().y() + this->pos().y()) / 16; if (x != last_x || y != last_y) { - clicking = false; if (editor->selected_events->contains(this)) { for (DraggablePixmapItem *item : *editor->selected_events) { item->move(x - last_x, y - last_y); @@ -1532,7 +1542,7 @@ void Editor::redrawObject(DraggablePixmapItem *item) { if (selected_events && selected_events->contains(item)) { QImage image = item->pixmap().toImage(); QPainter painter(&image); - painter.setPen(QColor(250, 100, 25)); + painter.setPen(QColor(250, 0, 255)); painter.drawRect(0, 0, image.width() - 1, image.height() - 1); painter.end(); item->setPixmap(QPixmap::fromImage(image)); @@ -1589,39 +1599,19 @@ void Editor::deleteEvent(Event *event) { //updateSelectedObjects(); } -// dunno how to detect bubbling. QMouseEvent::isAccepted seems to always be true -// check if selected_events changed instead. this has the side effect of deselecting -// when you click on a selected event, since selected_events doesn't change. - -QList selected_events_test; -bool clicking = false; - +// It doesn't seem to be possible to prevent the mousePress event +// from triggering both event's DraggablePixmapItem and the background mousePress. +// Since the DraggablePixmapItem's event fires first, we can set a temp +// variable "selectingEvent" so that we can detect whether or not the user +// is clicking on the background instead of an event. void Editor::objectsView_onMousePress(QMouseEvent *event) { - clicking = true; - selected_events_test = *selected_events; -} - -void Editor::objectsView_onMouseMove(QMouseEvent *event) { - clicking = false; -} - -void Editor::objectsView_onMouseRelease(QMouseEvent *event) { - if (clicking) { - if (selected_events_test.length()) { - if (selected_events_test.length() == selected_events->length()) { - bool deselect = true; - for (int i = 0; i < selected_events_test.length(); i++) { - if (selected_events_test.at(i) != selected_events->at(i)) { - deselect = false; - break; - } - } - if (deselect) { - selected_events->clear(); - updateSelectedEvents(); - } - } - } - clicking = false; + bool multiSelect = event->modifiers() & Qt::ControlModifier; + if (!selectingEvent && !multiSelect && selected_events->length() > 1) { + DraggablePixmapItem *first = selected_events->first(); + selected_events->clear(); + selected_events->append(first); + updateSelectedEvents(); } + + selectingEvent = false; } diff --git a/editor.h b/editor.h index c096d1a5..360ec21f 100755 --- a/editor.h +++ b/editor.h @@ -150,13 +150,12 @@ public: Editor *editor = NULL; Event *event = NULL; QGraphicsItemAnimation *pos_anim = NULL; - DraggablePixmapItem(Event *event_) : QGraphicsPixmapItem(event_->pixmap) { + DraggablePixmapItem(Event *event_, Editor *editor_) : QGraphicsPixmapItem(event_->pixmap) { event = event_; + editor = editor_; updatePosition(); } bool active; - bool right_click; - bool clicking; int last_x; int last_y; void updatePosition() { diff --git a/graphicsview.cpp b/graphicsview.cpp index 361ec3a9..02204103 100755 --- a/graphicsview.cpp +++ b/graphicsview.cpp @@ -10,14 +10,8 @@ void GraphicsView::mousePressEvent(QMouseEvent *event) { void GraphicsView::mouseMoveEvent(QMouseEvent *event) { QGraphicsView::mouseMoveEvent(event); - if (editor) { - editor->objectsView_onMouseMove(event); - } } void GraphicsView::mouseReleaseEvent(QMouseEvent *event) { QGraphicsView::mouseReleaseEvent(event); - if (editor) { - editor->objectsView_onMouseRelease(event); - } } diff --git a/mainwindow.cpp b/mainwindow.cpp index 4cc3f760..1afa4d19 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -548,12 +548,19 @@ void MainWindow::addNewEvent(QString event_type) // Should probably just pass layout and let the editor work it out void MainWindow::updateSelectedObjects() { - QList *all_events = editor->getObjects(); - QList *events = all_events; + QList *events = NULL; if (editor->selected_events && editor->selected_events->length()) { events = editor->selected_events; + } else { + events = new QList; + if (all_events && all_events->length()) { + DraggablePixmapItem *selectedEvent = all_events->first(); + editor->selected_events->append(selectedEvent); + editor->redrawObject(selectedEvent); + events->append(selectedEvent); + } } QMap event_obj_gfx_constants = editor->project->getEventObjGfxConstants(); @@ -675,7 +682,7 @@ void MainWindow::updateSelectedObjects() { QWidget *widget = new QWidget(frame); QFormLayout *fl = new QFormLayout(widget); fl->setContentsMargins(9, 0, 9, 0); - QComboBox *combo = new QComboBox(widget); + NoScrollComboBox *combo = new NoScrollComboBox(widget); combo->setEditable(true); QString value = item->event->get(key); diff --git a/mainwindow.ui b/mainwindow.ui index 59f41023..08ce7819 100755 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -490,7 +490,10 @@ - + + + Qt::StrongFocus + true @@ -504,7 +507,10 @@ - + + + Qt::StrongFocus + true @@ -1989,6 +1995,11 @@ QToolButton
neweventtoolbutton.h
+ + NoScrollComboBox + QComboBox +
noscrollcombobox.h
+
diff --git a/noscrollcombobox.cpp b/noscrollcombobox.cpp new file mode 100644 index 00000000..723289d9 --- /dev/null +++ b/noscrollcombobox.cpp @@ -0,0 +1,15 @@ +#include "noscrollcombobox.h" + +NoScrollComboBox::NoScrollComboBox(QWidget *parent) + : QComboBox(parent) +{ + // Don't let scrolling hijack focus. + setFocusPolicy(Qt::StrongFocus); +} + +void NoScrollComboBox::wheelEvent(QWheelEvent *event) +{ + // Only allow scrolling to modify contents when it explicitly has focus. + if (hasFocus()) + QComboBox::wheelEvent(event); +} diff --git a/noscrollcombobox.h b/noscrollcombobox.h new file mode 100644 index 00000000..1291c7e9 --- /dev/null +++ b/noscrollcombobox.h @@ -0,0 +1,17 @@ +#ifndef NOSCROLLCOMBOBOX_H +#define NOSCROLLCOMBOBOX_H + +#include + +class NoScrollComboBox : public QComboBox +{ + Q_OBJECT + +public: + explicit NoScrollComboBox(QWidget *parent = nullptr); + void wheelEvent(QWheelEvent *event); + +private: +}; + +#endif // NOSCROLLCOMBOBOX_H diff --git a/noscrollspinbox.cpp b/noscrollspinbox.cpp new file mode 100644 index 00000000..026a145c --- /dev/null +++ b/noscrollspinbox.cpp @@ -0,0 +1,16 @@ +#include "noscrollspinbox.h" + +NoScrollSpinBox::NoScrollSpinBox(QWidget *parent) + : QSpinBox(parent) +{ + // Don't let scrolling hijack focus. + setFocusPolicy(Qt::StrongFocus); +} + +void NoScrollSpinBox::wheelEvent(QWheelEvent *event) +{ + // Only allow scrolling to modify contents when it explicitly has focus. + if (hasFocus()) + QSpinBox::wheelEvent(event); +} + diff --git a/noscrollspinbox.h b/noscrollspinbox.h new file mode 100644 index 00000000..d9361f6f --- /dev/null +++ b/noscrollspinbox.h @@ -0,0 +1,17 @@ +#ifndef NOSCROLLSPINBOX_H +#define NOSCROLLSPINBOX_H + +#include + +class NoScrollSpinBox : public QSpinBox +{ + Q_OBJECT + +public: + explicit NoScrollSpinBox(QWidget *parent = nullptr); + void wheelEvent(QWheelEvent *event); + +private: +}; + +#endif // NOSCROLLSPINBOX_H diff --git a/objectpropertiesframe.ui b/objectpropertiesframe.ui index c11aaa6d..70c22186 100755 --- a/objectpropertiesframe.ui +++ b/objectpropertiesframe.ui @@ -105,7 +105,10 @@
- + + + Qt::StrongFocus + -32768 @@ -129,7 +132,10 @@ - + + + Qt::StrongFocus + -32768 @@ -153,7 +159,10 @@ - + + + Qt::StrongFocus + 15 @@ -210,7 +219,7 @@ - + true @@ -220,6 +229,9 @@ 0 + + Qt::StrongFocus + true @@ -239,6 +251,18 @@ + + + NoScrollComboBox + QComboBox +
noscrollcombobox.h
+
+ + NoScrollSpinBox + QSpinBox +
noscrollspinbox.h
+
+
spinBox_x spinBox_y diff --git a/pretmap.pro b/pretmap.pro index 5e722c11..96014047 100755 --- a/pretmap.pro +++ b/pretmap.pro @@ -25,7 +25,9 @@ SOURCES += main.cpp\ objectpropertiesframe.cpp \ graphicsview.cpp \ parseutil.cpp \ - neweventtoolbutton.cpp + neweventtoolbutton.cpp \ + noscrollcombobox.cpp \ + noscrollspinbox.cpp HEADERS += mainwindow.h \ project.h \ @@ -39,7 +41,9 @@ HEADERS += mainwindow.h \ objectpropertiesframe.h \ graphicsview.h \ parseutil.h \ - neweventtoolbutton.h + neweventtoolbutton.h \ + noscrollcombobox.h \ + noscrollspinbox.h FORMS += mainwindow.ui \ objectpropertiesframe.ui