From b72931cc9e6a736241a266894249057c3f125782 Mon Sep 17 00:00:00 2001 From: Albert S Date: Sun, 13 Sep 2020 14:33:40 +0200 Subject: [PATCH] GUI: Begin basic drag/drop between buttons: Allow swapping places Issue: #7 --- entrypushbutton.cpp | 80 ++++++++++++++++++++++++++++++++++++++------- entrypushbutton.h | 21 ++++++++---- window.cpp | 36 ++++++++++++++++++++ window.h | 21 ++++++++---- 4 files changed, 133 insertions(+), 25 deletions(-) diff --git a/entrypushbutton.cpp b/entrypushbutton.cpp index cf0649c..a6ca7ea 100644 --- a/entrypushbutton.cpp +++ b/entrypushbutton.cpp @@ -13,6 +13,10 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ + +#include +#include +#include #include "entrypushbutton.h" EntryPushButton::EntryPushButton(const EntryConfig &config) : QPushButton() @@ -37,9 +41,6 @@ EntryPushButton::EntryPushButton(const EntryConfig &config) : QPushButton() connect(this, SIGNAL(clicked()), this, SLOT(emitOwnClicked())); } - - - void EntryPushButton::emitOwnClicked() { emit clicked(this->config); @@ -65,12 +66,67 @@ void EntryPushButton::showName() this->setText(this->config.name); } -int EntryPushButton::getRow() const { return config.row; } -int EntryPushButton::getCol() const { return config.col; } -QString EntryPushButton::getName() const { return config.name; } -QString EntryPushButton::getShortcutKey() const { return config.key; } -void EntryPushButton::setShortcutKey(QString key) { this->config.key = key; } -void EntryPushButton::setRow(int row) { this->config.row = row; } -void EntryPushButton::setCol(int col) { this->config.col = col; } -QStringList EntryPushButton::getArguments() const { return this->config.arguments; } -QString EntryPushButton::getCommand() const { return this->config.command; } +void EntryPushButton::mousePressEvent(QMouseEvent *event) +{ + if(event->button() == Qt::LeftButton) + { + dragStartPosition = event->pos(); + } + return QPushButton::mousePressEvent(event); +} + +void EntryPushButton::mouseMoveEvent(QMouseEvent *event) +{ + if(!(event->buttons() & Qt::LeftButton)) + { + return; + } + if((event->pos() - dragStartPosition).manhattanLength() < QApplication::startDragDistance()) + { + return; + } + + QDrag *drag = new QDrag(this); + QMimeData *mimeData = new QMimeData(); + QByteArray data; + mimeData->setData(ENTRYBUTTON_MIME_TYPE_STR, data); + drag->setMimeData(mimeData); + Qt::DropAction dropAction = drag->exec(Qt::MoveAction); +} + +int EntryPushButton::getRow() const +{ + return config.row; +} +int EntryPushButton::getCol() const +{ + return config.col; +} +QString EntryPushButton::getName() const +{ + return config.name; +} +QString EntryPushButton::getShortcutKey() const +{ + return config.key; +} +void EntryPushButton::setShortcutKey(QString key) +{ + this->config.key = key; +} +void EntryPushButton::setRow(int row) +{ + this->config.row = row; +} +void EntryPushButton::setCol(int col) +{ + this->config.col = col; +} +QStringList EntryPushButton::getArguments() const +{ + return this->config.arguments; +} +QString EntryPushButton::getCommand() const +{ + return this->config.command; +} diff --git a/entrypushbutton.h b/entrypushbutton.h index e7c30c9..8c14852 100644 --- a/entrypushbutton.h +++ b/entrypushbutton.h @@ -18,18 +18,29 @@ #include #include +#include #include "entryprovider.h" + +#define ENTRYBUTTON_MIME_TYPE_STR "application/x-qsrun-entrypushbutton" + class EntryPushButton : public QPushButton { Q_OBJECT -private: + private: EntryConfig config; -private slots: + QPoint dragStartPosition; + + private slots: void emitOwnClicked(); -signals: + signals: void clicked(const EntryConfig &config); -public: + + protected: + void mousePressEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); + + public: EntryPushButton(const EntryConfig &config); const EntryConfig &getEntryConfig(); void setEntryConfig(const EntryConfig &config); @@ -46,6 +57,4 @@ public: void setShortcutKey(QString key); }; - - #endif // ENTRYPUSHBUTTON_H diff --git a/window.cpp b/window.cpp index e2f4cd3..c861d47 100644 --- a/window.cpp +++ b/window.cpp @@ -37,6 +37,7 @@ Window::Window(EntryProvider &entryProvider, SettingsProvider &configProvider) createGui(); initFromConfig(); this->lineEdit->installEventFilter(this); + this->setAcceptDrops(true); QFont font; font.setPointSize(48); font.setBold(true); @@ -405,3 +406,38 @@ void Window::focusInput() { this->lineEdit->setFocus(); } + +void Window::dragEnterEvent(QDragEnterEvent *event) +{ + if(event->mimeData()->hasFormat(ENTRYBUTTON_MIME_TYPE_STR)) + { + event->acceptProposedAction(); + } +} + +void Window::dropEvent(QDropEvent *event) +{ + int count = grid->count(); + for(int i = 0; i < count; i++) + { + QLayoutItem *current = grid->itemAt(i); + if(current->geometry().contains(event->pos())) + { + EntryPushButton *buttonAtDrop = (EntryPushButton *)current->widget(); + EntryPushButton *buttonAtSource = (EntryPushButton *)event->source(); + + int tmp_row = buttonAtSource->getRow(); + int tmp_col = buttonAtSource->getCol(); + + grid->addWidget(buttonAtSource, buttonAtDrop->getRow(), buttonAtDrop->getCol()); + buttonAtSource->setRow(buttonAtDrop->getRow()); + buttonAtSource->setCol(buttonAtDrop->getCol()); + + grid->addWidget(buttonAtDrop, tmp_row, tmp_col); + buttonAtDrop->setRow(tmp_row); + buttonAtDrop->setCol(tmp_col); + break; + } + } + event->acceptProposedAction(); +} diff --git a/window.h b/window.h index 62ac26b..8ef965c 100644 --- a/window.h +++ b/window.h @@ -30,6 +30,9 @@ #include #include #include +#include +#include +#include #include "entrypushbutton.h" #include "calculationengine.h" #include "settingsprovider.h" @@ -37,13 +40,17 @@ class Window : public QWidget { Q_OBJECT -private: + protected: + void dragEnterEvent(QDragEnterEvent *event); + void dropEvent(QDropEvent *event); + + private: EntryProvider *entryProvider; SettingsProvider *settingsProvider; CalculationEngine calcEngine; QString calculationresult; - QVector userEntryButtons; - QVector systemEntryButtons; + QVector userEntryButtons; + QVector systemEntryButtons; QVector buttonsInGrid; QLabel calculationResultLabel; QString currentCalculationResult; @@ -63,20 +70,20 @@ private: void lineEditTextChanged(QString text); void addPATHSuggestion(const QString &text); void clearGrid(); - void addCalcResult(const QString & expression); + void addCalcResult(const QString &expression); void initTreeWidgets(); QStringList generatePATHSuggestions(const QString &text); void closeWindow(); -private slots: + private slots: void lineEditReturnPressed(); void showCalculationResultContextMenu(const QPoint &point); -public: + + public: Window(EntryProvider &entryProvider, SettingsProvider &settingsProvider); void setSystemConfig(const QVector &config); bool eventFilter(QObject *obj, QEvent *event); void focusInput(); ~Window(); - }; #endif