GUI: Begin basic drag/drop between buttons: Allow swapping places

Issue: #7
This commit is contained in:
Albert S. 2020-09-13 14:33:40 +02:00
parent b588fd07be
commit b72931cc9e
4 changed files with 133 additions and 25 deletions

View File

@ -13,6 +13,10 @@
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#include <QDrag>
#include <QMimeData>
#include <QApplication>
#include "entrypushbutton.h" #include "entrypushbutton.h"
EntryPushButton::EntryPushButton(const EntryConfig &config) : QPushButton() EntryPushButton::EntryPushButton(const EntryConfig &config) : QPushButton()
@ -37,9 +41,6 @@ EntryPushButton::EntryPushButton(const EntryConfig &config) : QPushButton()
connect(this, SIGNAL(clicked()), this, SLOT(emitOwnClicked())); connect(this, SIGNAL(clicked()), this, SLOT(emitOwnClicked()));
} }
void EntryPushButton::emitOwnClicked() void EntryPushButton::emitOwnClicked()
{ {
emit clicked(this->config); emit clicked(this->config);
@ -65,12 +66,67 @@ void EntryPushButton::showName()
this->setText(this->config.name); this->setText(this->config.name);
} }
int EntryPushButton::getRow() const { return config.row; } void EntryPushButton::mousePressEvent(QMouseEvent *event)
int EntryPushButton::getCol() const { return config.col; } {
QString EntryPushButton::getName() const { return config.name; } if(event->button() == Qt::LeftButton)
QString EntryPushButton::getShortcutKey() const { return config.key; } {
void EntryPushButton::setShortcutKey(QString key) { this->config.key = key; } dragStartPosition = event->pos();
void EntryPushButton::setRow(int row) { this->config.row = row; } }
void EntryPushButton::setCol(int col) { this->config.col = col; } return QPushButton::mousePressEvent(event);
QStringList EntryPushButton::getArguments() const { return this->config.arguments; } }
QString EntryPushButton::getCommand() const { return this->config.command; }
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;
}

View File

@ -18,17 +18,28 @@
#include <QWidget> #include <QWidget>
#include <QPushButton> #include <QPushButton>
#include <QMouseEvent>
#include "entryprovider.h" #include "entryprovider.h"
#define ENTRYBUTTON_MIME_TYPE_STR "application/x-qsrun-entrypushbutton"
class EntryPushButton : public QPushButton class EntryPushButton : public QPushButton
{ {
Q_OBJECT Q_OBJECT
private: private:
EntryConfig config; EntryConfig config;
QPoint dragStartPosition;
private slots: private slots:
void emitOwnClicked(); void emitOwnClicked();
signals: signals:
void clicked(const EntryConfig &config); void clicked(const EntryConfig &config);
protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
public: public:
EntryPushButton(const EntryConfig &config); EntryPushButton(const EntryConfig &config);
const EntryConfig &getEntryConfig(); const EntryConfig &getEntryConfig();
@ -46,6 +57,4 @@ public:
void setShortcutKey(QString key); void setShortcutKey(QString key);
}; };
#endif // ENTRYPUSHBUTTON_H #endif // ENTRYPUSHBUTTON_H

View File

@ -37,6 +37,7 @@ Window::Window(EntryProvider &entryProvider, SettingsProvider &configProvider)
createGui(); createGui();
initFromConfig(); initFromConfig();
this->lineEdit->installEventFilter(this); this->lineEdit->installEventFilter(this);
this->setAcceptDrops(true);
QFont font; QFont font;
font.setPointSize(48); font.setPointSize(48);
font.setBold(true); font.setBold(true);
@ -405,3 +406,38 @@ void Window::focusInput()
{ {
this->lineEdit->setFocus(); 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();
}

View File

@ -30,6 +30,9 @@
#include <QThread> #include <QThread>
#include <QTreeWidget> #include <QTreeWidget>
#include <QLabel> #include <QLabel>
#include <QMimeData>
#include <QDebug>
#include <QRect>
#include "entrypushbutton.h" #include "entrypushbutton.h"
#include "calculationengine.h" #include "calculationengine.h"
#include "settingsprovider.h" #include "settingsprovider.h"
@ -37,6 +40,10 @@
class Window : public QWidget class Window : public QWidget
{ {
Q_OBJECT Q_OBJECT
protected:
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);
private: private:
EntryProvider *entryProvider; EntryProvider *entryProvider;
SettingsProvider *settingsProvider; SettingsProvider *settingsProvider;
@ -70,13 +77,13 @@ private:
private slots: private slots:
void lineEditReturnPressed(); void lineEditReturnPressed();
void showCalculationResultContextMenu(const QPoint &point); void showCalculationResultContextMenu(const QPoint &point);
public: public:
Window(EntryProvider &entryProvider, SettingsProvider &settingsProvider); Window(EntryProvider &entryProvider, SettingsProvider &settingsProvider);
void setSystemConfig(const QVector<EntryConfig> &config); void setSystemConfig(const QVector<EntryConfig> &config);
bool eventFilter(QObject *obj, QEvent *event); bool eventFilter(QObject *obj, QEvent *event);
void focusInput(); void focusInput();
~Window(); ~Window();
}; };
#endif #endif