summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Swift/QtUI/QtChatTabs.cpp2
-rw-r--r--Swift/QtUI/Trellis/QtGridSelectionDialog.cpp76
-rw-r--r--Swift/QtUI/Trellis/QtGridSelectionDialog.h8
3 files changed, 49 insertions, 37 deletions
diff --git a/Swift/QtUI/QtChatTabs.cpp b/Swift/QtUI/QtChatTabs.cpp
index de9d2d2..ad95a07 100644
--- a/Swift/QtUI/QtChatTabs.cpp
+++ b/Swift/QtUI/QtChatTabs.cpp
@@ -355,61 +355,61 @@ void QtChatTabs::handleTabTitleUpdated(QWidget* widget) {
if (tab) {
unreads.push_back(std::pair<std::string, int>(Q2PSTRING(tab->windowTitle()), tab->getCount()));
}
}
std::string current(Q2PSTRING(qobject_cast<QtTabbable*>(dynamicGrid_->currentWidget())->windowTitle()));
ChatMessageSummarizer summary;
QString title = summary.getSummary(current, unreads).c_str();
setWindowTitle(title);
emit onTitleChanged(title);
}
void QtChatTabs::flash() {
#ifndef SWIFTEN_PLATFORM_MACOSX
QApplication::alert(this, 0);
#endif
}
void QtChatTabs::handleOpenLayoutChangeDialog() {
disconnect(gridSelectionDialog_, SIGNAL(currentGridSizeChanged(QSize)), dynamicGrid_, SLOT(setDimensions(QSize)));
gridSelectionDialog_->setCurrentGridSize(dynamicGrid_->getDimension());
int screen = QApplication::desktop()->screenNumber(QCursor::pos());
QPoint center = QApplication::desktop()->screenGeometry(screen).center();
gridSelectionDialog_->move(center);
connect(gridSelectionDialog_, SIGNAL(currentGridSizeChanged(QSize)), dynamicGrid_, SLOT(setDimensions(QSize)));
gridSelectionDialog_->show();
QPoint pos(gridSelectionDialog_->getFrameSize().width() / 2, gridSelectionDialog_->getFrameSize().height() / 2);
- QCursor::setPos(gridSelectionDialog_->windowHandle()->screen(), gridSelectionDialog_->mapToGlobal(QPoint(gridSelectionDialog_->width(), gridSelectionDialog_->height()) - pos));
+ QCursor::setPos(gridSelectionDialog_->windowHandle()->screen(), gridSelectionDialog_->mapToGlobal(QPoint(gridSelectionDialog_->width(), (gridSelectionDialog_->height() - gridSelectionDialog_->getDescriptionTextHeight())) - pos));
}
void QtChatTabs::storeTabPositions() {
// save size
QByteArray gridSizeData;
QDataStream dataStreamGridSize(&gridSizeData, QIODevice::ReadWrite);
dataStreamGridSize << dynamicGrid_->getDimension();
settingsProvider_->storeSetting(QtUISettingConstants::TRELLIS_GRID_SIZE, Q2PSTRING(QString(gridSizeData.toBase64())));
// save positions
QByteArray tabPositionsData;
QDataStream dataStreamTabPositions(&tabPositionsData, QIODevice::ReadWrite);
dynamicGrid_->updateTabPositions();
dataStreamTabPositions << dynamicGrid_->getTabPositions();
settingsProvider_->storeSetting(QtUISettingConstants::TRELLIS_GRID_POSITIONS, Q2PSTRING(QString(tabPositionsData.toBase64())));
}
void QtChatTabs::resizeEvent(QResizeEvent*) {
emit geometryChanged();
}
void QtChatTabs::moveEvent(QMoveEvent*) {
emit geometryChanged();
}
void QtChatTabs::checkForFirstShow() {
if (!isVisible()) {
#ifndef Q_OS_MAC
showMinimized();
#else
diff --git a/Swift/QtUI/Trellis/QtGridSelectionDialog.cpp b/Swift/QtUI/Trellis/QtGridSelectionDialog.cpp
index fb9734d..0533edf 100644
--- a/Swift/QtUI/Trellis/QtGridSelectionDialog.cpp
+++ b/Swift/QtUI/Trellis/QtGridSelectionDialog.cpp
@@ -5,182 +5,190 @@
*/
#include <Swift/QtUI/Trellis/QtGridSelectionDialog.h>
#include <QApplication>
#include <QCursor>
#include <QPaintEvent>
#include <QPainter>
#include <QStyle>
#include <QStyleOption>
namespace Swift {
QtGridSelectionDialog::QtGridSelectionDialog(QWidget* parent) : QWidget(parent), descriptionText(tr("Select the number of rows and columns for your layout. You can change the size by moving the mouse or cursor keys.")) {
frameSize = QSize(28,28) * 2;
maxGridSize = QSize(7,7);
minGridSize = QSize(1,1);
currentGridSize = QSize(1,1);
padding = 4;
setWindowFlags(Qt::FramelessWindowHint);
setCursor(Qt::SizeAllCursor);
horizontalMargin = style()->pixelMetric(QStyle::PM_LayoutLeftMargin);
verticalMargin = style()->pixelMetric(QStyle::PM_LayoutBottomMargin);
}
QSize QtGridSelectionDialog::sizeHint() const {
// PM_MenuVMargin | frameSize | ( padding | frameSize ) * | PM_MenuVMargin
int width = horizontalMargin + frameSize.width() + (padding + frameSize.width()) * (currentGridSize.width() - 1) + horizontalMargin;
int height = verticalMargin + frameSize.height() + (padding + frameSize.height()) * (currentGridSize.height() - 1) + verticalMargin;
- // Add space for descriptive centered text below.
- auto fontMetrics = QFontMetrics(QApplication::font());
- auto descriptionBB = fontMetrics.boundingRect(QRect(0,0,width - 2*horizontalMargin,1000), Qt::TextWordWrap, descriptionText, 0, 0);
-
- height += descriptionBB.height() + descriptionBB.y();
+ height += getDescriptionTextHeight(width);
return QSize(width, height);
}
void QtGridSelectionDialog::setCurrentGridSize(const QSize& size) {
currentGridSize = size;
emit currentGridSizeChanged(size);
}
QSize QtGridSelectionDialog::getCurrentGridSize() const {
return currentGridSize;
}
void QtGridSelectionDialog::setMinGridSize(const QSize& size) {
minGridSize = size;
emit minGridSizeChanged(size);
}
QSize QtGridSelectionDialog::getMinGridSize() const {
return minGridSize;
}
void QtGridSelectionDialog::setMaxGridSize(const QSize& size) {
maxGridSize = size;
emit maxGridSizeChanged(size);
}
QSize QtGridSelectionDialog::getMaxGridSize() const {
return maxGridSize;
}
QSize QtGridSelectionDialog::getFrameSize() const {
return frameSize;
}
+int QtGridSelectionDialog::getDescriptionTextHeight() const {
+ auto width = horizontalMargin + frameSize.width() + (padding + frameSize.width()) * (currentGridSize.width() - 1) + horizontalMargin;
+ return getDescriptionTextHeight(width);
+}
+
+int QtGridSelectionDialog::getDescriptionTextHeight(int width) const {
+ // Height of descriptive centered text below trellis
+ auto fontMetrics = QFontMetrics(QApplication::font());
+ auto descriptionBB = fontMetrics.boundingRect(QRect(0, 0, width - 2 * horizontalMargin, 1000), Qt::TextWordWrap, descriptionText, 0, 0);
+
+ return (descriptionBB.height() + descriptionBB.y());
+}
+
void QtGridSelectionDialog::keyReleaseEvent(QKeyEvent* event) {
if (event) {
QSize newGridSize = currentGridSize;
if (event->key() == Qt::Key_Up) {
newGridSize += QSize(0, -1);
}
else if (event->key() == Qt::Key_Down) {
newGridSize += QSize(0, 1);
}
else if (event->key() == Qt::Key_Left) {
newGridSize += QSize(-1, 0);
}
else if (event->key() == Qt::Key_Right) {
newGridSize += QSize(1, 0);
}
else if (event->key() == Qt::Key_Return) {
hide();
setCurrentGridSize(currentGridSize);
}
else if (event->key() == Qt::Key_Escape) {
hide();
}
if (minGridSize.expandedTo(newGridSize).boundedTo(maxGridSize) != currentGridSize) {
currentGridSize = minGridSize.expandedTo(newGridSize).boundedTo(maxGridSize);
QSize newSizeHint = sizeHint();
resize(newSizeHint);
- QCursor::setPos(mapToGlobal(QPoint(newSizeHint.width(), newSizeHint.height()) - QPoint(frameSize.width() / 2, frameSize.height() / 2)));
+ QCursor::setPos(mapToGlobal(QPoint(newSizeHint.width(), newSizeHint.height()- getDescriptionTextHeight()) - QPoint(frameSize.width() / 2, frameSize.height() / 2)));
}
}
}
void QtGridSelectionDialog::mousePressEvent(QMouseEvent*) {
hide();
setCurrentGridSize(currentGridSize);
}
void QtGridSelectionDialog::paintEvent(QPaintEvent*) {
QPainter painter(this);
// draw grid
QRect gridCell = QRect(QPoint(0,0), frameSize);
painter.setBrush(palette().highlight());
painter.setPen(Qt::NoPen);
for (int x = 0; x < currentGridSize.width(); x++) {
for (int y = 0; y < currentGridSize.height(); y++) {
int xPos = horizontalMargin + (x * (frameSize.width() + padding));
int yPos = verticalMargin + (y * (frameSize.height() + padding));
gridCell.moveTo(QPoint(xPos, yPos));
painter.drawRect(gridCell);
}
}
// draw description text
auto fontMetrics = QFontMetrics(QApplication::font());
auto descriptionBB = fontMetrics.boundingRect(QRect(0,0, width() - 2 * horizontalMargin,0), Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, descriptionText, 0, 0);
QStyleOption opt;
opt.initFrom(this);
int textY = verticalMargin + (currentGridSize.height() * (frameSize.height() + padding));
int textX = (size().width() - descriptionBB.width()) / 2;
style()->drawItemText(&painter, QRect(textX, textY, descriptionBB.width(), descriptionBB.height()), Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, opt.palette, true, descriptionText, foregroundRole());
}
void QtGridSelectionDialog::showEvent(QShowEvent*) {
- setMouseTracking(true);
+ timerId = startTimer(1000 / 25);
}
void QtGridSelectionDialog::hideEvent(QHideEvent*) {
- setMouseTracking(false);
-}
-
-void QtGridSelectionDialog::mouseMoveEvent(QMouseEvent*) {
- QPoint diff = (frameGeometry().bottomRight() - QCursor::pos());
- QSize newDimensions = currentGridSize;
- if (diff.x() > frameSize.width() * 1.5) {
- newDimensions -= QSize(diff.x() / (frameSize.width() * 1.5), 0);
- }
- if (diff.y() > frameSize.height() * 1.5) {
- newDimensions -= QSize(0, diff.y() / (frameSize.height() * 1.5));
- }
- if (minGridSize.expandedTo(newDimensions).boundedTo(maxGridSize) != currentGridSize) {
- currentGridSize = minGridSize.expandedTo(newDimensions).boundedTo(maxGridSize);
- resize(sizeHint());
- }
-}
+ killTimer(timerId);
+}
+
+void QtGridSelectionDialog::timerEvent(QTimerEvent*) {
+
+ const QPoint diff = QCursor::pos() - frameGeometry().topLeft() - QPoint(horizontalMargin, verticalMargin);
+ const auto toleranceFactor = 4; // Ratio of how far (1/tolerance) the mouse should move for the next frame to be plotted
+ // dx, dy - mouse position with respect to first top-left square
+ const auto dx = diff.x();
+ const auto dy = diff.y();
+ // width, height - dimension of each square with padding
+ const auto width = frameSize.width() + padding;
+ const auto height = frameSize.height() + padding;
+ // xThreshold, yThreshold - how far the mouse should be moved so that a new square is added or the existing one is hidden
+ const auto xThreshold = width / toleranceFactor;
+ const auto yThreshold = height / toleranceFactor;
+
+ const auto getSize = [](int length, int threshold, int delta) {
+ if (delta < length + threshold) {
+ return 1;
+ }
+ else {
+ return (delta + (length - threshold)) / length;
+ }
+ };
+ const QSize newGridSize(getSize(width, xThreshold, dx), getSize(height, yThreshold, dy));
-void QtGridSelectionDialog::leaveEvent(QEvent *) {
- QPoint diff = (frameGeometry().bottomRight() - QCursor::pos());
- QSize newGridSize = currentGridSize;
- if (diff.x() < 0) {
- newGridSize += QSize(1,0);
- }
- if (diff.y() < 0) {
- newGridSize += QSize(0,1);
- }
if (minGridSize.expandedTo(newGridSize).boundedTo(maxGridSize) != currentGridSize) {
currentGridSize = minGridSize.expandedTo(newGridSize).boundedTo(maxGridSize);
resize(sizeHint());
}
}
-
bool QtGridSelectionDialog::event(QEvent* event) {
// Hide the window when it becomes a non-top-level window.
if (event->type() == QEvent::WindowDeactivate) {
hide();
return true;
}
return QWidget::event(event);
}
}
diff --git a/Swift/QtUI/Trellis/QtGridSelectionDialog.h b/Swift/QtUI/Trellis/QtGridSelectionDialog.h
index 3ccba55..2cb36ca 100644
--- a/Swift/QtUI/Trellis/QtGridSelectionDialog.h
+++ b/Swift/QtUI/Trellis/QtGridSelectionDialog.h
@@ -3,60 +3,64 @@
* All rights reserved.
* See the COPYING file for more information.
*/
#pragma once
#include <QSize>
#include <QWidget>
namespace Swift {
class QtGridSelectionDialog : public QWidget {
Q_OBJECT
Q_PROPERTY(QSize currentGridSize READ getCurrentGridSize WRITE setCurrentGridSize NOTIFY currentGridSizeChanged)
Q_PROPERTY(QSize minGridSize READ getMinGridSize WRITE setMinGridSize NOTIFY minGridSizeChanged)
Q_PROPERTY(QSize maxGridSize READ getMaxGridSize WRITE setMaxGridSize NOTIFY maxGridSizeChanged)
public:
explicit QtGridSelectionDialog(QWidget* parent = nullptr);
virtual QSize sizeHint() const;
void setCurrentGridSize(const QSize& size);
QSize getCurrentGridSize() const;
void setMinGridSize(const QSize& size);
QSize getMinGridSize() const;
void setMaxGridSize(const QSize& size);
QSize getMaxGridSize() const;
QSize getFrameSize() const;
+ int getDescriptionTextHeight() const;
signals:
void currentGridSizeChanged(QSize);
void minGridSizeChanged(QSize);
void maxGridSizeChanged(QSize);
protected:
void keyReleaseEvent(QKeyEvent* event);
void mousePressEvent(QMouseEvent* event);
- void mouseMoveEvent(QMouseEvent* event);
void paintEvent(QPaintEvent* event);
void showEvent(QShowEvent* event);
void hideEvent(QHideEvent* event);
- void leaveEvent(QEvent *event);
bool event(QEvent* event);
+ void timerEvent(QTimerEvent* event);
+
+ private:
+ int getDescriptionTextHeight(int width) const;
private:
int padding;
int horizontalMargin;
int verticalMargin;
+ int timerId;
QSize frameSize;
QSize currentGridSize;
QSize minGridSize;
QSize maxGridSize;
const QString descriptionText;
};
}