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
|
||||
* 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,17 +18,28 @@
|
||||
|
||||
#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:
|
||||
EntryConfig config;
|
||||
QPoint dragStartPosition;
|
||||
|
||||
private slots:
|
||||
void emitOwnClicked();
|
||||
|
||||
signals:
|
||||
void clicked(const EntryConfig &config);
|
||||
|
||||
protected:
|
||||
void mousePressEvent(QMouseEvent *event);
|
||||
void mouseMoveEvent(QMouseEvent *event);
|
||||
|
||||
public:
|
||||
EntryPushButton(const EntryConfig &config);
|
||||
const EntryConfig &getEntryConfig();
|
||||
@ -46,6 +57,4 @@ public:
|
||||
void setShortcutKey(QString key);
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif // ENTRYPUSHBUTTON_H
|
||||
|
36
window.cpp
36
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();
|
||||
}
|
||||
|
9
window.h
9
window.h
@ -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,6 +40,10 @@
|
||||
class Window : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
protected:
|
||||
void dragEnterEvent(QDragEnterEvent *event);
|
||||
void dropEvent(QDropEvent *event);
|
||||
|
||||
private:
|
||||
EntryProvider *entryProvider;
|
||||
SettingsProvider *settingsProvider;
|
||||
@ -70,13 +77,13 @@ private:
|
||||
private slots:
|
||||
void lineEditReturnPressed();
|
||||
void showCalculationResultContextMenu(const QPoint &point);
|
||||
|
||||
public:
|
||||
Window(EntryProvider &entryProvider, SettingsProvider &settingsProvider);
|
||||
void setSystemConfig(const QVector<EntryConfig> &config);
|
||||
bool eventFilter(QObject *obj, QEvent *event);
|
||||
void focusInput();
|
||||
~Window();
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user