Skip to content

Commit 5fa01e7

Browse files
authored
Navigate workspace in the main window by dragging the mouse (LMMS#7595)
The scroll bars shown in the main window were removed in favor of a new navigation feature that utilizes dragging the mouse instead. Users can now hold the mouse down on an empty part of the workspace and drag the mouse around to navigate where necessary.
1 parent ef1d86f commit 5fa01e7

File tree

2 files changed

+81
-14
lines changed

2 files changed

+81
-14
lines changed

include/MainWindow.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <QTimer>
3030
#include <QList>
3131
#include <QMainWindow>
32+
#include <QMdiArea>
3233

3334
#include "ConfigManager.h"
3435

@@ -57,7 +58,7 @@ class MainWindow : public QMainWindow
5758
public:
5859
QMdiArea* workspace()
5960
{
60-
return m_workspace;
61+
return static_cast<QMdiArea*>(m_workspace);
6162
}
6263

6364
QWidget* toolBar()
@@ -203,7 +204,22 @@ private slots:
203204
bool guiSaveProject();
204205
bool guiSaveProjectAs( const QString & filename );
205206

206-
QMdiArea * m_workspace;
207+
class MovableQMdiArea : public QMdiArea
208+
{
209+
public:
210+
MovableQMdiArea(QWidget* parent = nullptr);
211+
~MovableQMdiArea() {}
212+
protected:
213+
void mousePressEvent(QMouseEvent* event) override;
214+
void mouseMoveEvent(QMouseEvent* event) override;
215+
void mouseReleaseEvent(QMouseEvent* event) override;
216+
private:
217+
bool m_isBeingMoved;
218+
int m_lastX;
219+
int m_lastY;
220+
};
221+
222+
MovableQMdiArea * m_workspace;
207223

208224
QWidget * m_toolBar;
209225
QGridLayout * m_toolBarLayout;
@@ -258,7 +274,6 @@ private slots:
258274

259275
} ;
260276

261-
262277
} // namespace gui
263278

264279
} // namespace lmms

src/gui/MainWindow.cpp

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ MainWindow::MainWindow() :
159159
sideBar->appendTab(new FileBrowser(root_paths.join("*"), FileItem::defaultFilters(), title,
160160
embed::getIconPixmap("computer").transformed(QTransform().rotate(90)), splitter, dirs_as_items));
161161

162-
m_workspace = new QMdiArea(splitter);
162+
m_workspace = new MovableQMdiArea(splitter);
163163

164164
// Load background
165165
emit initProgress(tr("Loading background picture"));
@@ -179,8 +179,8 @@ MainWindow::MainWindow() :
179179
}
180180

181181
m_workspace->setOption( QMdiArea::DontMaximizeSubWindowOnActivation );
182-
m_workspace->setHorizontalScrollBarPolicy( Qt::ScrollBarAsNeeded );
183-
m_workspace->setVerticalScrollBarPolicy( Qt::ScrollBarAsNeeded );
182+
m_workspace->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
183+
m_workspace->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
184184

185185
hbox->addWidget(sideBar);
186186
hbox->addWidget(splitter);
@@ -531,8 +531,6 @@ void MainWindow::finalize()
531531
}
532532

533533

534-
535-
536534
int MainWindow::addWidgetToToolBar( QWidget * _w, int _row, int _col )
537535
{
538536
int col = ( _col == -1 ) ? m_toolBarLayout->columnCount() + 7 : _col;
@@ -944,12 +942,6 @@ void MainWindow::toggleWindow( QWidget *window, bool forceShow )
944942
parent->hide();
945943
refocus();
946944
}
947-
948-
// Workaround for Qt Bug #260116
949-
m_workspace->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
950-
m_workspace->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
951-
m_workspace->setHorizontalScrollBarPolicy( Qt::ScrollBarAsNeeded );
952-
m_workspace->setVerticalScrollBarPolicy( Qt::ScrollBarAsNeeded );
953945
}
954946

955947

@@ -1601,4 +1593,64 @@ void MainWindow::onProjectFileNameChanged()
16011593
}
16021594

16031595

1596+
MainWindow::MovableQMdiArea::MovableQMdiArea(QWidget* parent) :
1597+
QMdiArea(parent),
1598+
m_isBeingMoved(false),
1599+
m_lastX(0),
1600+
m_lastY(0)
1601+
{}
1602+
1603+
void MainWindow::MovableQMdiArea::mousePressEvent(QMouseEvent* event)
1604+
{
1605+
m_lastX = event->x();
1606+
m_lastY = event->y();
1607+
m_isBeingMoved = true;
1608+
setCursor(Qt::ClosedHandCursor);
1609+
}
1610+
1611+
void MainWindow::MovableQMdiArea::mouseMoveEvent(QMouseEvent* event)
1612+
{
1613+
if (m_isBeingMoved == false) { return; }
1614+
1615+
int minXBoundary = window()->width() - 100;
1616+
int maxXBoundary = 100;
1617+
int minYBoundary = window()->height() - 100;
1618+
int maxYBoundary = 100;
1619+
1620+
int minX = minXBoundary;
1621+
int maxX = maxXBoundary;
1622+
int minY = minYBoundary;
1623+
int maxY = maxYBoundary;
1624+
1625+
auto subWindows = subWindowList();
1626+
for (auto* curWindow : subWindows)
1627+
{
1628+
if (curWindow->isVisible())
1629+
{
1630+
minX = std::min(minX, curWindow->x());
1631+
maxX = std::max(maxX, curWindow->x() + curWindow->width());
1632+
minY = std::min(minY, curWindow->y());
1633+
maxY = std::max(maxY, curWindow->y() + curWindow->height());
1634+
}
1635+
}
1636+
1637+
int scrollX = m_lastX - event->x();
1638+
int scrollY = m_lastY - event->y();
1639+
1640+
scrollX = scrollX < 0 && minX >= minXBoundary ? 0 : scrollX;
1641+
scrollX = scrollX > 0 && maxX <= maxXBoundary ? 0 : scrollX;
1642+
scrollY = scrollY < 0 && minY >= minYBoundary ? 0 : scrollY;
1643+
scrollY = scrollY > 0 && maxY <= maxYBoundary ? 0 : scrollY;
1644+
1645+
scrollContentsBy(-scrollX, -scrollY);
1646+
m_lastX = event->x();
1647+
m_lastY = event->y();
1648+
}
1649+
1650+
void MainWindow::MovableQMdiArea::mouseReleaseEvent(QMouseEvent* event)
1651+
{
1652+
setCursor(Qt::ArrowCursor);
1653+
m_isBeingMoved = false;
1654+
}
1655+
16041656
} // namespace lmms::gui

0 commit comments

Comments
 (0)