Browse Source

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

Marcus Huderle 6 years ago
parent
commit
dc6b82b4dc
11 changed files with 158 additions and 64 deletions
  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 View File

4
 #include <QPainter>
4
 #include <QPainter>
5
 #include <QMouseEvent>
5
 #include <QMouseEvent>
6
 
6
 
7
+bool selectingEvent = false;
8
+
7
 Editor::Editor(Ui::MainWindow* ui)
9
 Editor::Editor(Ui::MainWindow* ui)
8
 {
10
 {
9
     this->ui = ui;
11
     this->ui = ui;
469
         delete events_group;
471
         delete events_group;
470
     }
472
     }
471
 
473
 
474
+    selected_events->clear();
475
+
472
     events_group = new EventGroup;
476
     events_group = new EventGroup;
473
     scene->addItem(events_group);
477
     scene->addItem(events_group);
474
 
478
 
484
 }
488
 }
485
 
489
 
486
 DraggablePixmapItem *Editor::addMapEvent(Event *event) {
490
 DraggablePixmapItem *Editor::addMapEvent(Event *event) {
487
-    DraggablePixmapItem *object = new DraggablePixmapItem(event);
488
-    object->editor = this;
491
+    DraggablePixmapItem *object = new DraggablePixmapItem(event, this);
489
     events_group->addToGroup(object);
492
     events_group->addToGroup(object);
490
     return object;
493
     return object;
491
 }
494
 }
808
 
811
 
809
 void Editor::updatePrimaryTileset(QString tilesetLabel)
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
 void Editor::updateSecondaryTileset(QString tilesetLabel)
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
 void MetatilesPixmapItem::paintTileChanged(Map *map) {
832
 void MetatilesPixmapItem::paintTileChanged(Map *map) {
1471
 
1480
 
1472
 void DraggablePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *mouse) {
1481
 void DraggablePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *mouse) {
1473
     active = true;
1482
     active = true;
1474
-    clicking = true;
1475
     last_x = (mouse->pos().x() + this->pos().x()) / 16;
1483
     last_x = (mouse->pos().x() + this->pos().x()) / 16;
1476
     last_y = (mouse->pos().y() + this->pos().y()) / 16;
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
     //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("y"));
1488
     //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("y"));
1478
 }
1489
 }
1479
 
1490
 
1489
         int x = (mouse->pos().x() + this->pos().x()) / 16;
1500
         int x = (mouse->pos().x() + this->pos().x()) / 16;
1490
         int y = (mouse->pos().y() + this->pos().y()) / 16;
1501
         int y = (mouse->pos().y() + this->pos().y()) / 16;
1491
         if (x != last_x || y != last_y) {
1502
         if (x != last_x || y != last_y) {
1492
-            clicking = false;
1493
             if (editor->selected_events->contains(this)) {
1503
             if (editor->selected_events->contains(this)) {
1494
                 for (DraggablePixmapItem *item : *editor->selected_events) {
1504
                 for (DraggablePixmapItem *item : *editor->selected_events) {
1495
                     item->move(x - last_x, y - last_y);
1505
                     item->move(x - last_x, y - last_y);
1532
         if (selected_events && selected_events->contains(item)) {
1542
         if (selected_events && selected_events->contains(item)) {
1533
             QImage image = item->pixmap().toImage();
1543
             QImage image = item->pixmap().toImage();
1534
             QPainter painter(&image);
1544
             QPainter painter(&image);
1535
-            painter.setPen(QColor(250, 100, 25));
1545
+            painter.setPen(QColor(250, 0, 255));
1536
             painter.drawRect(0, 0, image.width() - 1, image.height() - 1);
1546
             painter.drawRect(0, 0, image.width() - 1, image.height() - 1);
1537
             painter.end();
1547
             painter.end();
1538
             item->setPixmap(QPixmap::fromImage(image));
1548
             item->setPixmap(QPixmap::fromImage(image));
1589
     //updateSelectedObjects();
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
 void Editor::objectsView_onMousePress(QMouseEvent *event) {
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 View File

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

+ 0
- 6
graphicsview.cpp View File

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

+ 10
- 3
mainwindow.cpp View File

548
 
548
 
549
 // Should probably just pass layout and let the editor work it out
549
 // Should probably just pass layout and let the editor work it out
550
 void MainWindow::updateSelectedObjects() {
550
 void MainWindow::updateSelectedObjects() {
551
-
552
     QList<DraggablePixmapItem *> *all_events = editor->getObjects();
551
     QList<DraggablePixmapItem *> *all_events = editor->getObjects();
553
-    QList<DraggablePixmapItem *> *events = all_events;
552
+    QList<DraggablePixmapItem *> *events = NULL;
554
 
553
 
555
     if (editor->selected_events && editor->selected_events->length()) {
554
     if (editor->selected_events && editor->selected_events->length()) {
556
         events = editor->selected_events;
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
     QMap<QString, int> event_obj_gfx_constants = editor->project->getEventObjGfxConstants();
566
     QMap<QString, int> event_obj_gfx_constants = editor->project->getEventObjGfxConstants();
675
             QWidget *widget = new QWidget(frame);
682
             QWidget *widget = new QWidget(frame);
676
             QFormLayout *fl = new QFormLayout(widget);
683
             QFormLayout *fl = new QFormLayout(widget);
677
             fl->setContentsMargins(9, 0, 9, 0);
684
             fl->setContentsMargins(9, 0, 9, 0);
678
-            QComboBox *combo = new QComboBox(widget);
685
+            NoScrollComboBox *combo = new NoScrollComboBox(widget);
679
             combo->setEditable(true);
686
             combo->setEditable(true);
680
 
687
 
681
             QString value = item->event->get(key);
688
             QString value = item->event->get(key);

+ 13
- 2
mainwindow.ui View File

490
                      </widget>
490
                      </widget>
491
                     </item>
491
                     </item>
492
                     <item row="0" column="1">
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
                       <property name="editable">
497
                       <property name="editable">
495
                        <bool>true</bool>
498
                        <bool>true</bool>
496
                       </property>
499
                       </property>
504
                      </widget>
507
                      </widget>
505
                     </item>
508
                     </item>
506
                     <item row="1" column="1">
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
                       <property name="editable">
514
                       <property name="editable">
509
                        <bool>true</bool>
515
                        <bool>true</bool>
510
                       </property>
516
                       </property>
1989
    <extends>QToolButton</extends>
1995
    <extends>QToolButton</extends>
1990
    <header>neweventtoolbutton.h</header>
1996
    <header>neweventtoolbutton.h</header>
1991
   </customwidget>
1997
   </customwidget>
1998
+  <customwidget>
1999
+   <class>NoScrollComboBox</class>
2000
+   <extends>QComboBox</extends>
2001
+   <header>noscrollcombobox.h</header>
2002
+  </customwidget>
1992
  </customwidgets>
2003
  </customwidgets>
1993
  <resources>
2004
  <resources>
1994
   <include location="resources/images.qrc"/>
2005
   <include location="resources/images.qrc"/>

+ 15
- 0
noscrollcombobox.cpp View File

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 View File

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 View File

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 View File

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 View File

105
              </widget>
105
              </widget>
106
             </item>
106
             </item>
107
             <item>
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
               <property name="minimum">
112
               <property name="minimum">
110
                <number>-32768</number>
113
                <number>-32768</number>
111
               </property>
114
               </property>
129
              </widget>
132
              </widget>
130
             </item>
133
             </item>
131
             <item>
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
               <property name="minimum">
139
               <property name="minimum">
134
                <number>-32768</number>
140
                <number>-32768</number>
135
               </property>
141
               </property>
153
              </widget>
159
              </widget>
154
             </item>
160
             </item>
155
             <item>
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
               <property name="maximum">
166
               <property name="maximum">
158
                <number>15</number>
167
                <number>15</number>
159
               </property>
168
               </property>
210
        </widget>
219
        </widget>
211
       </item>
220
       </item>
212
       <item row="0" column="1">
221
       <item row="0" column="1">
213
-       <widget class="QComboBox" name="comboBox_sprite">
222
+       <widget class="NoScrollComboBox" name="comboBox_sprite">
214
         <property name="enabled">
223
         <property name="enabled">
215
          <bool>true</bool>
224
          <bool>true</bool>
216
         </property>
225
         </property>
220
           <verstretch>0</verstretch>
229
           <verstretch>0</verstretch>
221
          </sizepolicy>
230
          </sizepolicy>
222
         </property>
231
         </property>
232
+        <property name="focusPolicy">
233
+         <enum>Qt::StrongFocus</enum>
234
+        </property>
223
         <property name="editable">
235
         <property name="editable">
224
          <bool>true</bool>
236
          <bool>true</bool>
225
         </property>
237
         </property>
239
    </item>
251
    </item>
240
   </layout>
252
   </layout>
241
  </widget>
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
  <tabstops>
266
  <tabstops>
243
   <tabstop>spinBox_x</tabstop>
267
   <tabstop>spinBox_x</tabstop>
244
   <tabstop>spinBox_y</tabstop>
268
   <tabstop>spinBox_y</tabstop>

+ 6
- 2
pretmap.pro View File

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