refs #750, allow to reset window position of dock widget

This commit is contained in:
Klaus Basan
2016-08-29 18:33:00 +02:00
parent bfd94ea6a8
commit d2aa2e9c1c
4 changed files with 73 additions and 10 deletions

View File

@@ -31,6 +31,8 @@
#include <QWidget>
#include <Qt>
#include <QtGlobal>
#include <QApplication>
#include <QDesktopWidget>
using namespace BlackMisc;
using namespace BlackGui::Components;
@@ -339,10 +341,12 @@ namespace BlackGui
contextMenu->addAction(BlackMisc::CIcons::floatOne16(), "Float", this, &CDockWidget::toggleFloating);
}
// Margin actions
// State actions (windows state)
contextMenu->addAction(BlackMisc::CIcons::load16(), "Restore", this, &CDockWidget::restoreFromSettings);
contextMenu->addAction(BlackMisc::CIcons::save16(), "Save state", this, &CDockWidget::saveToSettings);
contextMenu->addAction(BlackMisc::CIcons::save16(), "Reset to defaults", this, &CDockWidget::resetSettings);
contextMenu->addAction(BlackMisc::CIcons::refresh16(), "Reset to defaults", this, &CDockWidget::resetSettings);
contextMenu->addAction(BlackMisc::CIcons::refresh16(), "Reset position", this, &CDockWidget::resetPosition);
this->m_input->setMargins(this->contentsMargins());
contextMenu->addAction(BlackMisc::CIcons::tableSheet16(), "Margins", this, &CDockWidget::ps_dummy);
contextMenu->addAction(this->m_marginMenuAction);
@@ -587,4 +591,11 @@ namespace BlackGui
this->setSettings(s);
this->restoreFromSettings();
}
void CDockWidget::resetPosition()
{
// center on screen when floating
if (!this->isFloating()) { return; }
this->move(QApplication::desktop()->screen()->rect().center() - this->rect().center());
}
} // namespace

View File

@@ -123,6 +123,9 @@ namespace BlackGui
//! Reset margin settings
void resetSettings();
//! Reset window position
void resetPosition();
//! Set title and internally keep a backup
void setWindowTitle(const QString &title);

View File

@@ -15,6 +15,7 @@
#include "blackgui/stylesheetutility.h"
#include "blackmisc/icons.h"
#include "blackmisc/logmessage.h"
#include "blackmisc/verify.h"
#include <QAction>
#include <QCloseEvent>
@@ -100,9 +101,12 @@ namespace BlackGui
QMenu *subMenuToggleFloat = new QMenu("Toggle Float/Dock", menu);
QMenu *subMenuDisplay = new QMenu("Display", menu);
QMenu *subMenuRestore = new QMenu("Restore", menu);
QMenu *subMenuResetPositions = new QMenu("Reset position", menu);
subMenuRestore->setIcon(CIcons::load16());
subMenuResetPositions->setIcon(CIcons::refresh16());
subMenuRestore->addActions(this->getInfoAreaRestoreActions(subMenuRestore));
subMenuDisplay->addActions(this->getInfoAreaSelectActions(subMenuDisplay));
subMenuResetPositions->addActions(this->getInfoAreaResetPositionActions(subMenuResetPositions));
QSignalMapper *signalMapperToggleFloating = new QSignalMapper(menu);
bool c = false; // check connections
@@ -131,6 +135,7 @@ namespace BlackGui
menu->addMenu(subMenuDisplay);
menu->addMenu(subMenuToggleFloat);
menu->addMenu(subMenuResetPositions);
menu->addMenu(subMenuRestore);
// where and how to display tab bar
@@ -164,7 +169,6 @@ namespace BlackGui
{
if (!infoArea) { return false; }
if (infoArea->isFloating()) { return false; }
return infoArea == this->getSelectedDockInfoArea();
}
@@ -220,6 +224,25 @@ namespace BlackGui
return actions;
}
QList<QAction *> CInfoArea::getInfoAreaResetPositionActions(QWidget *parent) const
{
Q_ASSERT(parent);
int i = 0;
QList<QAction *> actions;
for (const CDockWidgetInfoArea *dockWidgetInfoArea : m_dockWidgetInfoAreas)
{
const QPixmap pm = this->indexToPixmap(i);
const QString wt(dockWidgetInfoArea->windowTitleBackup());
QAction *action = new QAction(QIcon(pm), wt, parent);
action->setData(i);
action->setObjectName(this->objectName().append(":getInfoAreaResetPositionActions:").append(wt));
connect(action, &QAction::triggered, this, &CInfoArea::resetPositionByAction);
actions.append(action);
i++;
}
return actions;
}
QList<QAction *> CInfoArea::getInfoAreaToggleFloatingActions(QWidget *parent) const
{
Q_ASSERT(parent);
@@ -326,8 +349,8 @@ namespace BlackGui
{
if (!this->isValidAreaIndex(areaIndex)) { return; }
CDockWidgetInfoArea *dw = this->m_dockWidgetInfoAreas.at(areaIndex);
Q_ASSERT(dw);
if (!dw) return;
BLACK_VERIFY_X(dw, Q_FUNC_INFO, "Missing info area");
if (!dw) { return; }
dw->toggleFloating();
}
@@ -335,15 +358,15 @@ namespace BlackGui
{
if (!this->isValidAreaIndex(areaIndex)) { return; }
CDockWidgetInfoArea *dw = this->m_dockWidgetInfoAreas.at(areaIndex);
Q_ASSERT(dw);
if (!dw) return;
BLACK_VERIFY_X(dw, Q_FUNC_INFO, "Missing info area");
if (!dw) { return; }
dw->toggleVisibility();
}
void CInfoArea::selectArea(int areaIndex)
{
CDockWidgetInfoArea *dw = this->m_dockWidgetInfoAreas.at(areaIndex);
Q_ASSERT(dw);
BLACK_VERIFY_X(dw, Q_FUNC_INFO, "Missing info area");
if (!dw) { return; }
Q_ASSERT(this->m_tabBar);
if (m_tabBar->count() < 1) { return; }
@@ -358,20 +381,35 @@ namespace BlackGui
}
}
void CInfoArea::resetPosition(int areaIndex)
{
CDockWidgetInfoArea *dw = this->m_dockWidgetInfoAreas.at(areaIndex);
BLACK_VERIFY_X(dw, Q_FUNC_INFO, "Missing info area");
if (!dw) { return; }
dw->resetPosition();
}
void CInfoArea::selectAreaByAction()
{
const QObject *sender = QObject::sender();
Q_ASSERT(sender);
const QAction *action = qobject_cast<const QAction *>(sender);
Q_ASSERT(action);
int index = action->data().toInt();
this->selectArea(index);
}
void CInfoArea::resetPositionByAction()
{
const QObject *sender = QObject::sender();
const QAction *action = qobject_cast<const QAction *>(sender);
Q_ASSERT(action);
int index = action->data().toInt();
this->resetPosition(index);
}
void CInfoArea::toggleAreaFloatingByAction()
{
const QObject *sender = QObject::sender();
Q_ASSERT(sender);
const QAction *action = qobject_cast<const QAction *>(sender);
Q_ASSERT(action);
int index = action->data().toInt();

View File

@@ -84,6 +84,11 @@ namespace BlackGui
//! \param parent which will own the action (deletion)
QList<QAction *> getInfoAreaRestoreActions(QWidget *parent) const;
//! Create a list of actions to reset the position the info areas.
//! This could be used in a menu or somewhere else.
//! \param parent which will own the action (deletion)
QList<QAction *> getInfoAreaResetPositionActions(QWidget *parent) const;
//! Docked area indexes
QList<int> getAreaIndexesDockedOrFloating(bool floating) const;
@@ -119,12 +124,18 @@ namespace BlackGui
//! Select area
void selectArea(int areaIndex);
//! Reset position
void resetPosition(int areaIndex);
//! Set current tab bar index by given widget
void selectArea(const CDockWidgetInfoArea *dockWidgetInfoArea);
//! Select area (sender is QAction)
void selectAreaByAction();
//! Reset window position of area (sender is QAction)
void resetPositionByAction();
//! Toggle area floating (sender is QAction)
void toggleAreaFloatingByAction();