2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-01-30 00:17:39 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello <sithlord48@gmail.com>
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2012 Symless Ltd.
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2008 Volker Lanz <vl@fidra.de>
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2012-06-10 16:50:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "ScreenSetupView.h"
|
2024-08-08 17:41:59 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
#include "ScreenSetupModel.h"
|
2024-11-07 05:09:12 +00:00
|
|
|
#include "dialogs/ScreenSettingsDialog.h"
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-21 03:26:16 +00:00
|
|
|
#include <QDrag>
|
|
|
|
|
#include <QDragEnterEvent>
|
|
|
|
|
#include <QDragMoveEvent>
|
2024-07-02 19:07:06 +00:00
|
|
|
#include <QHeaderView>
|
2024-10-21 03:26:16 +00:00
|
|
|
#include <QMimeData>
|
|
|
|
|
#include <QMouseEvent>
|
|
|
|
|
#include <QResizeEvent>
|
2018-06-24 16:22:21 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
ScreenSetupView::ScreenSetupView(QWidget *parent) : QTableView(parent)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
setDropIndicatorShown(true);
|
|
|
|
|
setDragDropMode(DragDrop);
|
|
|
|
|
setSelectionMode(SingleSelection);
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2025-01-30 00:17:39 +00:00
|
|
|
setIconSize(QSize(96, 96));
|
2024-07-02 19:07:06 +00:00
|
|
|
horizontalHeader()->hide();
|
|
|
|
|
verticalHeader()->hide();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ScreenSetupView::setModel(QAbstractItemModel *model)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
QTableView::setModel(model);
|
|
|
|
|
setTableSize();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
ScreenSetupModel *ScreenSetupView::model() const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
return qobject_cast<ScreenSetupModel *>(QTableView::model());
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-30 04:45:28 +00:00
|
|
|
void ScreenSetupView::showScreenConfig(int col, int row)
|
|
|
|
|
{
|
|
|
|
|
ScreenSettingsDialog dlg(this, &model()->screen(col, row), &model()->m_Screens);
|
|
|
|
|
dlg.exec();
|
|
|
|
|
Q_EMIT model()->screensChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ScreenSetupView::setTableSize()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
for (int i = 0; i < model()->columnCount(); i++)
|
|
|
|
|
setColumnWidth(i, width() / model()->columnCount());
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
for (int i = 0; i < model()->rowCount(); i++)
|
|
|
|
|
setRowHeight(i, height() / model()->rowCount());
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ScreenSetupView::resizeEvent(QResizeEvent *event)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
setTableSize();
|
|
|
|
|
event->ignore();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ScreenSetupView::mouseDoubleClickEvent(QMouseEvent *event)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
if (event->buttons() & Qt::LeftButton) {
|
|
|
|
|
int col = columnAt(event->pos().x());
|
|
|
|
|
int row = rowAt(event->pos().y());
|
|
|
|
|
|
|
|
|
|
if (!model()->screen(col, row).isNull()) {
|
2025-01-30 04:45:28 +00:00
|
|
|
showScreenConfig(col, row);
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
} else
|
|
|
|
|
event->ignore();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ScreenSetupView::dragEnterEvent(QDragEnterEvent *event)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
// we accept anything that enters us by a drag as long as the
|
|
|
|
|
// mime type is okay. anything else is dealt with in dragMoveEvent()
|
|
|
|
|
if (event->mimeData()->hasFormat(ScreenSetupModel::mimeType()))
|
|
|
|
|
event->accept();
|
|
|
|
|
else
|
|
|
|
|
event->ignore();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ScreenSetupView::dragMoveEvent(QDragMoveEvent *event)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
if (event->mimeData()->hasFormat(ScreenSetupModel::mimeType())) {
|
|
|
|
|
// where does the event come from? myself or someone else?
|
|
|
|
|
if (event->source() == this) {
|
|
|
|
|
// myself is ok, but then it must be a move action, never a copy
|
|
|
|
|
event->setDropAction(Qt::MoveAction);
|
|
|
|
|
event->accept();
|
|
|
|
|
} else {
|
2024-08-01 00:13:01 +00:00
|
|
|
const auto &point = event->position().toPoint();
|
|
|
|
|
int col = columnAt(point.x());
|
|
|
|
|
int row = rowAt(point.y());
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
// a drop from outside is not allowed if there's a screen already there.
|
|
|
|
|
if (!model()->screen(col, row).isNull())
|
2016-12-28 11:50:32 +00:00
|
|
|
event->ignore();
|
2025-01-30 04:45:28 +00:00
|
|
|
else {
|
2024-07-02 19:07:06 +00:00
|
|
|
event->acceptProposedAction();
|
2025-01-30 04:45:28 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
event->ignore();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// this is reimplemented from QAbstractItemView::startDrag()
|
2024-10-17 18:04:35 +00:00
|
|
|
void ScreenSetupView::startDrag(Qt::DropActions)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
QModelIndexList indexes = selectedIndexes();
|
|
|
|
|
|
|
|
|
|
if (indexes.count() != 1)
|
|
|
|
|
return;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
QMimeData *pData = model()->mimeData(indexes);
|
2025-04-27 19:39:23 +00:00
|
|
|
if (pData == nullptr)
|
2024-07-02 19:07:06 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const QPixmap &pixmap = model()->screen(indexes[0]).pixmap();
|
|
|
|
|
QDrag *pDrag = new QDrag(this);
|
|
|
|
|
pDrag->setPixmap(pixmap);
|
|
|
|
|
pDrag->setMimeData(pData);
|
|
|
|
|
pDrag->setHotSpot(QPoint(pixmap.width() / 2, pixmap.height() / 2));
|
|
|
|
|
|
|
|
|
|
if (pDrag->exec(Qt::MoveAction, Qt::MoveAction) == Qt::MoveAction) {
|
|
|
|
|
selectionModel()->clear();
|
|
|
|
|
|
|
|
|
|
// make sure to only delete the drag source if screens weren't swapped
|
|
|
|
|
// see ScreenSetupModel::dropMimeData
|
|
|
|
|
if (!model()->screen(indexes[0]).swapped())
|
|
|
|
|
model()->screen(indexes[0]) = Screen();
|
|
|
|
|
else
|
|
|
|
|
model()->screen(indexes[0]).setSwapped(false);
|
|
|
|
|
|
2024-10-21 04:08:10 +00:00
|
|
|
Q_EMIT model()->screensChanged();
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ScreenSetupView::initViewItemOption(QStyleOptionViewItem *option) const
|
|
|
|
|
{
|
2025-01-30 00:17:39 +00:00
|
|
|
// HACK make a basic widget and init from it
|
|
|
|
|
auto w = new QWidget();
|
|
|
|
|
option->initFrom(w);
|
|
|
|
|
w->deleteLater();
|
|
|
|
|
delete w;
|
|
|
|
|
|
|
|
|
|
option->decorationSize = QSize(96, 96);
|
2024-07-08 14:37:51 +00:00
|
|
|
option->showDecorationSelected = true;
|
|
|
|
|
option->decorationPosition = QStyleOptionViewItem::Top;
|
2025-01-30 00:17:39 +00:00
|
|
|
option->decorationAlignment = Qt::AlignHCenter | Qt::AlignVCenter;
|
|
|
|
|
option->displayAlignment = Qt::AlignTop | Qt::AlignHCenter;
|
2024-07-08 14:37:51 +00:00
|
|
|
option->textElideMode = Qt::ElideMiddle;
|
2024-10-21 03:26:16 +00:00
|
|
|
}
|