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
父節點 b588fd07be
當前提交 b72931cc9e
共有 4 個文件被更改,包括 133 次插入25 次删除

查看文件

@ -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 <QDrag>
#include <QMimeData>
#include <QApplication>
#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;
}

查看文件

@ -18,18 +18,29 @@
#include <QWidget>
#include <QPushButton>
#include <QMouseEvent>
#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

查看文件

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

查看文件

@ -30,6 +30,9 @@
#include <QThread>
#include <QTreeWidget>
#include <QLabel>
#include <QMimeData>
#include <QDebug>
#include <QRect>
#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<EntryPushButton*> userEntryButtons;
QVector<EntryPushButton*> systemEntryButtons;
QVector<EntryPushButton *> userEntryButtons;
QVector<EntryPushButton *> systemEntryButtons;
QVector<EntryPushButton *> 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<EntryConfig> &config);
bool eventFilter(QObject *obj, QEvent *event);
void focusInput();
~Window();
};
#endif