GUI: Begin basic drag/drop between buttons: Allow swapping places
Issue: #7
This commit is contained in:
parent
b588fd07be
commit
b72931cc9e
@ -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;
|
||||||
|
}
|
||||||
|
@ -18,18 +18,29 @@
|
|||||||
|
|
||||||
#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;
|
||||||
private slots:
|
QPoint dragStartPosition;
|
||||||
|
|
||||||
|
private slots:
|
||||||
void emitOwnClicked();
|
void emitOwnClicked();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void clicked(const EntryConfig &config);
|
void clicked(const EntryConfig &config);
|
||||||
public:
|
|
||||||
|
protected:
|
||||||
|
void mousePressEvent(QMouseEvent *event);
|
||||||
|
void mouseMoveEvent(QMouseEvent *event);
|
||||||
|
|
||||||
|
public:
|
||||||
EntryPushButton(const EntryConfig &config);
|
EntryPushButton(const EntryConfig &config);
|
||||||
const EntryConfig &getEntryConfig();
|
const EntryConfig &getEntryConfig();
|
||||||
void setEntryConfig(const EntryConfig &config);
|
void setEntryConfig(const EntryConfig &config);
|
||||||
@ -46,6 +57,4 @@ public:
|
|||||||
void setShortcutKey(QString key);
|
void setShortcutKey(QString key);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif // ENTRYPUSHBUTTON_H
|
#endif // ENTRYPUSHBUTTON_H
|
||||||
|
36
window.cpp
36
window.cpp
@ -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();
|
||||||
|
}
|
||||||
|
21
window.h
21
window.h
@ -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,13 +40,17 @@
|
|||||||
class Window : public QWidget
|
class Window : public QWidget
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
private:
|
protected:
|
||||||
|
void dragEnterEvent(QDragEnterEvent *event);
|
||||||
|
void dropEvent(QDropEvent *event);
|
||||||
|
|
||||||
|
private:
|
||||||
EntryProvider *entryProvider;
|
EntryProvider *entryProvider;
|
||||||
SettingsProvider *settingsProvider;
|
SettingsProvider *settingsProvider;
|
||||||
CalculationEngine calcEngine;
|
CalculationEngine calcEngine;
|
||||||
QString calculationresult;
|
QString calculationresult;
|
||||||
QVector<EntryPushButton*> userEntryButtons;
|
QVector<EntryPushButton *> userEntryButtons;
|
||||||
QVector<EntryPushButton*> systemEntryButtons;
|
QVector<EntryPushButton *> systemEntryButtons;
|
||||||
QVector<EntryPushButton *> buttonsInGrid;
|
QVector<EntryPushButton *> buttonsInGrid;
|
||||||
QLabel calculationResultLabel;
|
QLabel calculationResultLabel;
|
||||||
QString currentCalculationResult;
|
QString currentCalculationResult;
|
||||||
@ -63,20 +70,20 @@ private:
|
|||||||
void lineEditTextChanged(QString text);
|
void lineEditTextChanged(QString text);
|
||||||
void addPATHSuggestion(const QString &text);
|
void addPATHSuggestion(const QString &text);
|
||||||
void clearGrid();
|
void clearGrid();
|
||||||
void addCalcResult(const QString & expression);
|
void addCalcResult(const QString &expression);
|
||||||
void initTreeWidgets();
|
void initTreeWidgets();
|
||||||
QStringList generatePATHSuggestions(const QString &text);
|
QStringList generatePATHSuggestions(const QString &text);
|
||||||
void closeWindow();
|
void closeWindow();
|
||||||
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
|
||||||
|
Loading…
Reference in New Issue
Block a user