浏览代码

Disable combobox scroll hijacking, and only show one event by default (rather than all)

Marcus Huderle 6 年前
父节点
当前提交
dc6b82b4dc
共有 11 个文件被更改,包括 158 次插入64 次删除
  1. 34
    44
      editor.cpp
  2. 2
    3
      editor.h
  3. 0
    6
      graphicsview.cpp
  4. 10
    3
      mainwindow.cpp
  5. 13
    2
      mainwindow.ui
  6. 15
    0
      noscrollcombobox.cpp
  7. 17
    0
      noscrollcombobox.h
  8. 16
    0
      noscrollspinbox.cpp
  9. 17
    0
      noscrollspinbox.h
  10. 28
    4
      objectpropertiesframe.ui
  11. 6
    2
      pretmap.pro

+ 34
- 44
editor.cpp 查看文件

@@ -4,6 +4,8 @@
4 4
 #include <QPainter>
5 5
 #include <QMouseEvent>
6 6
 
7
+bool selectingEvent = false;
8
+
7 9
 Editor::Editor(Ui::MainWindow* ui)
8 10
 {
9 11
     this->ui = ui;
@@ -469,6 +471,8 @@ void Editor::displayMapEvents() {
469 471
         delete events_group;
470 472
     }
471 473
 
474
+    selected_events->clear();
475
+
472 476
     events_group = new EventGroup;
473 477
     scene->addItem(events_group);
474 478
 
@@ -484,8 +488,7 @@ void Editor::displayMapEvents() {
484 488
 }
485 489
 
486 490
 DraggablePixmapItem *Editor::addMapEvent(Event *event) {
487
-    DraggablePixmapItem *object = new DraggablePixmapItem(event);
488
-    object->editor = this;
491
+    DraggablePixmapItem *object = new DraggablePixmapItem(event, this);
489 492
     events_group->addToGroup(object);
490 493
     return object;
491 494
 }
@@ -808,16 +811,22 @@ void Editor::updateDiveEmergeMap(QString mapName, QString direction) {
808 811
 
809 812
 void Editor::updatePrimaryTileset(QString tilesetLabel)
810 813
 {
811
-    map->layout->tileset_primary_label = tilesetLabel;
812
-    map->layout->tileset_primary = project->getTileset(tilesetLabel);
813
-    emit tilesetChanged(map->name);
814
+    if (map->layout->tileset_primary_label != tilesetLabel)
815
+    {
816
+        map->layout->tileset_primary_label = tilesetLabel;
817
+        map->layout->tileset_primary = project->getTileset(tilesetLabel);
818
+        emit tilesetChanged(map->name);
819
+    }
814 820
 }
815 821
 
816 822
 void Editor::updateSecondaryTileset(QString tilesetLabel)
817 823
 {
818
-    map->layout->tileset_secondary_label = tilesetLabel;
819
-    map->layout->tileset_secondary = project->getTileset(tilesetLabel);
820
-    emit tilesetChanged(map->name);
824
+    if (map->layout->tileset_secondary_label != tilesetLabel)
825
+    {
826
+        map->layout->tileset_secondary_label = tilesetLabel;
827
+        map->layout->tileset_secondary = project->getTileset(tilesetLabel);
828
+        emit tilesetChanged(map->name);
829
+    }
821 830
 }
822 831
 
823 832
 void MetatilesPixmapItem::paintTileChanged(Map *map) {
@@ -1471,9 +1480,11 @@ void CollisionPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
1471 1480
 
1472 1481
 void DraggablePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *mouse) {
1473 1482
     active = true;
1474
-    clicking = true;
1475 1483
     last_x = (mouse->pos().x() + this->pos().x()) / 16;
1476 1484
     last_y = (mouse->pos().y() + this->pos().y()) / 16;
1485
+    this->editor->selectMapEvent(this, mouse->modifiers() & Qt::ControlModifier);
1486
+    this->editor->updateSelectedEvents();
1487
+    selectingEvent = true;
1477 1488
     //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("y"));
1478 1489
 }
1479 1490
 
@@ -1489,7 +1500,6 @@ void DraggablePixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouse) {
1489 1500
         int x = (mouse->pos().x() + this->pos().x()) / 16;
1490 1501
         int y = (mouse->pos().y() + this->pos().y()) / 16;
1491 1502
         if (x != last_x || y != last_y) {
1492
-            clicking = false;
1493 1503
             if (editor->selected_events->contains(this)) {
1494 1504
                 for (DraggablePixmapItem *item : *editor->selected_events) {
1495 1505
                     item->move(x - last_x, y - last_y);
@@ -1532,7 +1542,7 @@ void Editor::redrawObject(DraggablePixmapItem *item) {
1532 1542
         if (selected_events && selected_events->contains(item)) {
1533 1543
             QImage image = item->pixmap().toImage();
1534 1544
             QPainter painter(&image);
1535
-            painter.setPen(QColor(250, 100, 25));
1545
+            painter.setPen(QColor(250, 0, 255));
1536 1546
             painter.drawRect(0, 0, image.width() - 1, image.height() - 1);
1537 1547
             painter.end();
1538 1548
             item->setPixmap(QPixmap::fromImage(image));
@@ -1589,39 +1599,19 @@ void Editor::deleteEvent(Event *event) {
1589 1599
     //updateSelectedObjects();
1590 1600
 }
1591 1601
 
1592
-// dunno how to detect bubbling. QMouseEvent::isAccepted seems to always be true
1593
-// check if selected_events changed instead. this has the side effect of deselecting
1594
-// when you click on a selected event, since selected_events doesn't change.
1595
-
1596
-QList<DraggablePixmapItem *> selected_events_test;
1597
-bool clicking = false;
1598
-
1602
+// It doesn't seem to be possible to prevent the mousePress event
1603
+// from triggering both event's DraggablePixmapItem and the background mousePress.
1604
+// Since the DraggablePixmapItem's event fires first, we can set a temp
1605
+// variable "selectingEvent" so that we can detect whether or not the user
1606
+// is clicking on the background instead of an event.
1599 1607
 void Editor::objectsView_onMousePress(QMouseEvent *event) {
1600
-    clicking = true;
1601
-    selected_events_test = *selected_events;
1602
-}
1603
-
1604
-void Editor::objectsView_onMouseMove(QMouseEvent *event) {
1605
-    clicking = false;
1606
-}
1607
-
1608
-void Editor::objectsView_onMouseRelease(QMouseEvent *event) {
1609
-    if (clicking) {
1610
-        if (selected_events_test.length()) {
1611
-            if (selected_events_test.length() == selected_events->length()) {
1612
-                bool deselect = true;
1613
-                for (int i = 0; i < selected_events_test.length(); i++) {
1614
-                    if (selected_events_test.at(i) != selected_events->at(i)) {
1615
-                        deselect = false;
1616
-                        break;
1617
-                    }
1618
-                }
1619
-                if (deselect) {
1620
-                    selected_events->clear();
1621
-                    updateSelectedEvents();
1622
-                }
1623
-            }
1624
-        }
1625
-        clicking = false;
1608
+    bool multiSelect = event->modifiers() & Qt::ControlModifier;
1609
+    if (!selectingEvent && !multiSelect && selected_events->length() > 1) {
1610
+        DraggablePixmapItem *first = selected_events->first();
1611
+        selected_events->clear();
1612
+        selected_events->append(first);
1613
+        updateSelectedEvents();
1626 1614
     }
1615
+
1616
+    selectingEvent = false;
1627 1617
 }

+ 2
- 3
editor.h 查看文件

@@ -150,13 +150,12 @@ public:
150 150
     Editor *editor = NULL;
151 151
     Event *event = NULL;
152 152
     QGraphicsItemAnimation *pos_anim = NULL;
153
-    DraggablePixmapItem(Event *event_) : QGraphicsPixmapItem(event_->pixmap) {
153
+    DraggablePixmapItem(Event *event_, Editor *editor_) : QGraphicsPixmapItem(event_->pixmap) {
154 154
         event = event_;
155
+        editor = editor_;
155 156
         updatePosition();
156 157
     }
157 158
     bool active;
158
-    bool right_click;
159
-    bool clicking;
160 159
     int last_x;
161 160
     int last_y;
162 161
     void updatePosition() {

+ 0
- 6
graphicsview.cpp 查看文件

@@ -10,14 +10,8 @@ void GraphicsView::mousePressEvent(QMouseEvent *event) {
10 10
 
11 11
 void GraphicsView::mouseMoveEvent(QMouseEvent *event) {
12 12
     QGraphicsView::mouseMoveEvent(event);
13
-    if (editor) {
14
-        editor->objectsView_onMouseMove(event);
15
-    }
16 13
 }
17 14
 
18 15
 void GraphicsView::mouseReleaseEvent(QMouseEvent *event) {
19 16
     QGraphicsView::mouseReleaseEvent(event);
20
-    if (editor) {
21
-        editor->objectsView_onMouseRelease(event);
22
-    }
23 17
 }

+ 10
- 3
mainwindow.cpp 查看文件

@@ -548,12 +548,19 @@ void MainWindow::addNewEvent(QString event_type)
548 548
 
549 549
 // Should probably just pass layout and let the editor work it out
550 550
 void MainWindow::updateSelectedObjects() {
551
-
552 551
     QList<DraggablePixmapItem *> *all_events = editor->getObjects();
553
-    QList<DraggablePixmapItem *> *events = all_events;
552
+    QList<DraggablePixmapItem *> *events = NULL;
554 553
 
555 554
     if (editor->selected_events && editor->selected_events->length()) {
556 555
         events = editor->selected_events;
556
+    } else {
557
+        events = new QList<DraggablePixmapItem*>;
558
+        if (all_events && all_events->length()) {
559
+            DraggablePixmapItem *selectedEvent = all_events->first();
560
+            editor->selected_events->append(selectedEvent);
561
+            editor->redrawObject(selectedEvent);
562
+            events->append(selectedEvent);
563
+        }
557 564
     }
558 565
 
559 566
     QMap<QString, int> event_obj_gfx_constants = editor->project->getEventObjGfxConstants();
@@ -675,7 +682,7 @@ void MainWindow::updateSelectedObjects() {
675 682
             QWidget *widget = new QWidget(frame);
676 683
             QFormLayout *fl = new QFormLayout(widget);
677 684
             fl->setContentsMargins(9, 0, 9, 0);
678
-            QComboBox *combo = new QComboBox(widget);
685
+            NoScrollComboBox *combo = new NoScrollComboBox(widget);
679 686
             combo->setEditable(true);
680 687
 
681 688
             QString value = item->event->get(key);

+ 13
- 2
mainwindow.ui 查看文件

@@ -490,7 +490,10 @@
490 490
                      </widget>
491 491
                     </item>
492 492
                     <item row="0" column="1">
493
-                     <widget class="QComboBox" name="comboBox_PrimaryTileset">
493
+                     <widget class="NoScrollComboBox" name="comboBox_PrimaryTileset">
494
+                      <property name="focusPolicy">
495
+                       <enum>Qt::StrongFocus</enum>
496
+                      </property>
494 497
                       <property name="editable">
495 498
                        <bool>true</bool>
496 499
                       </property>
@@ -504,7 +507,10 @@
504 507
                      </widget>
505 508
                     </item>
506 509
                     <item row="1" column="1">
507
-                     <widget class="QComboBox" name="comboBox_SecondaryTileset">
510
+                     <widget class="NoScrollComboBox" name="comboBox_SecondaryTileset">
511
+                      <property name="focusPolicy">
512
+                       <enum>Qt::StrongFocus</enum>
513
+                      </property>
508 514
                       <property name="editable">
509 515
                        <bool>true</bool>
510 516
                       </property>
@@ -1989,6 +1995,11 @@
1989 1995
    <extends>QToolButton</extends>
1990 1996
    <header>neweventtoolbutton.h</header>
1991 1997
   </customwidget>
1998
+  <customwidget>
1999
+   <class>NoScrollComboBox</class>
2000
+   <extends>QComboBox</extends>
2001
+   <header>noscrollcombobox.h</header>
2002
+  </customwidget>
1992 2003
  </customwidgets>
1993 2004
  <resources>
1994 2005
   <include location="resources/images.qrc"/>

+ 15
- 0
noscrollcombobox.cpp 查看文件

@@ -0,0 +1,15 @@
1
+#include "noscrollcombobox.h"
2
+
3
+NoScrollComboBox::NoScrollComboBox(QWidget *parent)
4
+    : QComboBox(parent)
5
+{
6
+    // Don't let scrolling hijack focus.
7
+    setFocusPolicy(Qt::StrongFocus);
8
+}
9
+
10
+void NoScrollComboBox::wheelEvent(QWheelEvent *event)
11
+{
12
+    // Only allow scrolling to modify contents when it explicitly has focus.
13
+    if (hasFocus())
14
+        QComboBox::wheelEvent(event);
15
+}

+ 17
- 0
noscrollcombobox.h 查看文件

@@ -0,0 +1,17 @@
1
+#ifndef NOSCROLLCOMBOBOX_H
2
+#define NOSCROLLCOMBOBOX_H
3
+
4
+#include <QComboBox>
5
+
6
+class NoScrollComboBox : public QComboBox
7
+{
8
+    Q_OBJECT
9
+
10
+public:
11
+    explicit NoScrollComboBox(QWidget *parent = nullptr);
12
+    void wheelEvent(QWheelEvent *event);
13
+
14
+private:
15
+};
16
+
17
+#endif // NOSCROLLCOMBOBOX_H

+ 16
- 0
noscrollspinbox.cpp 查看文件

@@ -0,0 +1,16 @@
1
+#include "noscrollspinbox.h"
2
+
3
+NoScrollSpinBox::NoScrollSpinBox(QWidget *parent)
4
+    : QSpinBox(parent)
5
+{
6
+    // Don't let scrolling hijack focus.
7
+    setFocusPolicy(Qt::StrongFocus);
8
+}
9
+
10
+void NoScrollSpinBox::wheelEvent(QWheelEvent *event)
11
+{
12
+    // Only allow scrolling to modify contents when it explicitly has focus.
13
+    if (hasFocus())
14
+        QSpinBox::wheelEvent(event);
15
+}
16
+

+ 17
- 0
noscrollspinbox.h 查看文件

@@ -0,0 +1,17 @@
1
+#ifndef NOSCROLLSPINBOX_H
2
+#define NOSCROLLSPINBOX_H
3
+
4
+#include <QSpinBox>
5
+
6
+class NoScrollSpinBox : public QSpinBox
7
+{
8
+    Q_OBJECT
9
+
10
+public:
11
+    explicit NoScrollSpinBox(QWidget *parent = nullptr);
12
+    void wheelEvent(QWheelEvent *event);
13
+
14
+private:
15
+};
16
+
17
+#endif // NOSCROLLSPINBOX_H

+ 28
- 4
objectpropertiesframe.ui 查看文件

@@ -105,7 +105,10 @@
105 105
              </widget>
106 106
             </item>
107 107
             <item>
108
-             <widget class="QSpinBox" name="spinBox_x">
108
+             <widget class="NoScrollSpinBox" name="spinBox_x">
109
+              <property name="focusPolicy">
110
+               <enum>Qt::StrongFocus</enum>
111
+              </property>
109 112
               <property name="minimum">
110 113
                <number>-32768</number>
111 114
               </property>
@@ -129,7 +132,10 @@
129 132
              </widget>
130 133
             </item>
131 134
             <item>
132
-             <widget class="QSpinBox" name="spinBox_y">
135
+             <widget class="NoScrollSpinBox" name="spinBox_y">
136
+              <property name="focusPolicy">
137
+               <enum>Qt::StrongFocus</enum>
138
+              </property>
133 139
               <property name="minimum">
134 140
                <number>-32768</number>
135 141
               </property>
@@ -153,7 +159,10 @@
153 159
              </widget>
154 160
             </item>
155 161
             <item>
156
-             <widget class="QSpinBox" name="spinBox_z">
162
+             <widget class="NoScrollSpinBox" name="spinBox_z">
163
+              <property name="focusPolicy">
164
+               <enum>Qt::StrongFocus</enum>
165
+              </property>
157 166
               <property name="maximum">
158 167
                <number>15</number>
159 168
               </property>
@@ -210,7 +219,7 @@
210 219
        </widget>
211 220
       </item>
212 221
       <item row="0" column="1">
213
-       <widget class="QComboBox" name="comboBox_sprite">
222
+       <widget class="NoScrollComboBox" name="comboBox_sprite">
214 223
         <property name="enabled">
215 224
          <bool>true</bool>
216 225
         </property>
@@ -220,6 +229,9 @@
220 229
           <verstretch>0</verstretch>
221 230
          </sizepolicy>
222 231
         </property>
232
+        <property name="focusPolicy">
233
+         <enum>Qt::StrongFocus</enum>
234
+        </property>
223 235
         <property name="editable">
224 236
          <bool>true</bool>
225 237
         </property>
@@ -239,6 +251,18 @@
239 251
    </item>
240 252
   </layout>
241 253
  </widget>
254
+ <customwidgets>
255
+  <customwidget>
256
+   <class>NoScrollComboBox</class>
257
+   <extends>QComboBox</extends>
258
+   <header>noscrollcombobox.h</header>
259
+  </customwidget>
260
+  <customwidget>
261
+   <class>NoScrollSpinBox</class>
262
+   <extends>QSpinBox</extends>
263
+   <header>noscrollspinbox.h</header>
264
+  </customwidget>
265
+ </customwidgets>
242 266
  <tabstops>
243 267
   <tabstop>spinBox_x</tabstop>
244 268
   <tabstop>spinBox_y</tabstop>

+ 6
- 2
pretmap.pro 查看文件

@@ -25,7 +25,9 @@ SOURCES += main.cpp\
25 25
     objectpropertiesframe.cpp \
26 26
     graphicsview.cpp \
27 27
     parseutil.cpp \
28
-    neweventtoolbutton.cpp
28
+    neweventtoolbutton.cpp \
29
+    noscrollcombobox.cpp \
30
+    noscrollspinbox.cpp
29 31
 
30 32
 HEADERS  += mainwindow.h \
31 33
     project.h \
@@ -39,7 +41,9 @@ HEADERS  += mainwindow.h \
39 41
     objectpropertiesframe.h \
40 42
     graphicsview.h \
41 43
     parseutil.h \
42
-    neweventtoolbutton.h
44
+    neweventtoolbutton.h \
45
+    noscrollcombobox.h \
46
+    noscrollspinbox.h
43 47
 
44 48
 FORMS    += mainwindow.ui \
45 49
     objectpropertiesframe.ui