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

This commit is contained in:
Marcus Huderle 2018-07-11 13:18:50 -05:00
parent 8930b2c01b
commit dc6b82b4dc
11 changed files with 158 additions and 64 deletions

View file

@ -4,6 +4,8 @@
#include <QPainter>
#include <QMouseEvent>
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)
{
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)
{
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<DraggablePixmapItem *> 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) {
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();
}
}
}
clicking = false;
}
selectingEvent = false;
}

View file

@ -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() {

View file

@ -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);
}
}

View file

@ -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<DraggablePixmapItem *> *all_events = editor->getObjects();
QList<DraggablePixmapItem *> *events = all_events;
QList<DraggablePixmapItem *> *events = NULL;
if (editor->selected_events && editor->selected_events->length()) {
events = editor->selected_events;
} else {
events = new QList<DraggablePixmapItem*>;
if (all_events && all_events->length()) {
DraggablePixmapItem *selectedEvent = all_events->first();
editor->selected_events->append(selectedEvent);
editor->redrawObject(selectedEvent);
events->append(selectedEvent);
}
}
QMap<QString, int> 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);

View file

@ -490,7 +490,10 @@
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="comboBox_PrimaryTileset">
<widget class="NoScrollComboBox" name="comboBox_PrimaryTileset">
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
<property name="editable">
<bool>true</bool>
</property>
@ -504,7 +507,10 @@
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="comboBox_SecondaryTileset">
<widget class="NoScrollComboBox" name="comboBox_SecondaryTileset">
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
<property name="editable">
<bool>true</bool>
</property>
@ -1989,6 +1995,11 @@
<extends>QToolButton</extends>
<header>neweventtoolbutton.h</header>
</customwidget>
<customwidget>
<class>NoScrollComboBox</class>
<extends>QComboBox</extends>
<header>noscrollcombobox.h</header>
</customwidget>
</customwidgets>
<resources>
<include location="resources/images.qrc"/>

15
noscrollcombobox.cpp Normal file
View file

@ -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);
}

17
noscrollcombobox.h Normal file
View file

@ -0,0 +1,17 @@
#ifndef NOSCROLLCOMBOBOX_H
#define NOSCROLLCOMBOBOX_H
#include <QComboBox>
class NoScrollComboBox : public QComboBox
{
Q_OBJECT
public:
explicit NoScrollComboBox(QWidget *parent = nullptr);
void wheelEvent(QWheelEvent *event);
private:
};
#endif // NOSCROLLCOMBOBOX_H

16
noscrollspinbox.cpp Normal file
View file

@ -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);
}

17
noscrollspinbox.h Normal file
View file

@ -0,0 +1,17 @@
#ifndef NOSCROLLSPINBOX_H
#define NOSCROLLSPINBOX_H
#include <QSpinBox>
class NoScrollSpinBox : public QSpinBox
{
Q_OBJECT
public:
explicit NoScrollSpinBox(QWidget *parent = nullptr);
void wheelEvent(QWheelEvent *event);
private:
};
#endif // NOSCROLLSPINBOX_H

View file

@ -105,7 +105,10 @@
</widget>
</item>
<item>
<widget class="QSpinBox" name="spinBox_x">
<widget class="NoScrollSpinBox" name="spinBox_x">
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
<property name="minimum">
<number>-32768</number>
</property>
@ -129,7 +132,10 @@
</widget>
</item>
<item>
<widget class="QSpinBox" name="spinBox_y">
<widget class="NoScrollSpinBox" name="spinBox_y">
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
<property name="minimum">
<number>-32768</number>
</property>
@ -153,7 +159,10 @@
</widget>
</item>
<item>
<widget class="QSpinBox" name="spinBox_z">
<widget class="NoScrollSpinBox" name="spinBox_z">
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
<property name="maximum">
<number>15</number>
</property>
@ -210,7 +219,7 @@
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="comboBox_sprite">
<widget class="NoScrollComboBox" name="comboBox_sprite">
<property name="enabled">
<bool>true</bool>
</property>
@ -220,6 +229,9 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
<property name="editable">
<bool>true</bool>
</property>
@ -239,6 +251,18 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>NoScrollComboBox</class>
<extends>QComboBox</extends>
<header>noscrollcombobox.h</header>
</customwidget>
<customwidget>
<class>NoScrollSpinBox</class>
<extends>QSpinBox</extends>
<header>noscrollspinbox.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>spinBox_x</tabstop>
<tabstop>spinBox_y</tabstop>

View file

@ -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