Add CNavigatorDialog

refs #503
This commit is contained in:
Roland Winklmeier
2016-01-17 17:07:01 +01:00
committed by Klaus Basan
parent a125b02984
commit 86ba56f75b
3 changed files with 316 additions and 0 deletions

View File

@@ -0,0 +1,165 @@
/* Copyright (C) 2016
* swift project Community / Contributors
*
* This file is part of swift Project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "navigatordialog.h"
#include "ui_navigatordialog.h"
#include "blackgui/infoarea.h"
#include "blackgui/guiutility.h"
#include "blackgui/stylesheetutility.h"
#include <QToolButton>
#include <QGridLayout>
#include <QAction>
using namespace BlackGui;
using namespace BlackMisc;
namespace BlackGui
{
namespace Components
{
CNavigatorDialog::CNavigatorDialog(QWidget *parent) :
QDialog(parent, Qt::Tool),
CEnableForFramelessWindow(CEnableForFramelessWindow::WindowTool, false, "NavigatorDialog", this),
ui(new Ui::CNavigatorDialog)
{
ui->setupUi(this);
// context menu
this->setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, &CNavigatorDialog::customContextMenuRequested, this, &CNavigatorDialog::ps_showContextMenu);
connect(&CStyleSheetUtility::instance(), &CStyleSheetUtility::styleSheetsChanged, this, &CNavigatorDialog::ps_onStyleSheetsChanged);
this->ps_onStyleSheetsChanged();
}
CNavigatorDialog::~CNavigatorDialog()
{ }
void CNavigatorDialog::buildNavigator(int columns)
{
if (m_firstBuild)
{
m_firstBuild = false;
this->insertOwnActions();
}
// remove old layout
CGuiUtility::deleteLayout(this->ui->fr_NavigatorDialogInner->layout(), false);
// new layout
QGridLayout *gridLayout = new QGridLayout(this->ui->fr_NavigatorDialogInner);
gridLayout->setObjectName("gl_CNavigatorDialog");
gridLayout->setSpacing(0);
gridLayout->setMargin(0);
gridLayout->setContentsMargins(0, 0, 0, 0);
this->ui->fr_NavigatorDialogInner->setLayout(gridLayout);
int r = 0;
int c = 0;
for (const auto &action : actions())
{
QToolButton *tb = new QToolButton(this->ui->fr_NavigatorDialogInner);
tb->setDefaultAction(action);
tb->setObjectName(this->objectName().append(":").append(action->objectName()));
gridLayout->addWidget(tb, r, c++);
tb->show();
if (c < columns) { continue; }
c = 0;
r++;
}
int w = 16 * gridLayout->columnCount();
int h = 16 * gridLayout->rowCount();
QSize min(w, h);
this->ui->fr_NavigatorDialogInner->setMinimumSize(min);
this->setMinimumSize(min);
this->adjustSize();
}
void CNavigatorDialog::toggleFrameless()
{
this->setFrameless(!this->isFrameless());
}
void CNavigatorDialog::ps_onStyleSheetsChanged()
{
const QString fn(CStyleSheetUtility::fileNameNavigator());
const QString qss(CStyleSheetUtility::instance().style(fn));
this->setStyleSheet(qss);
}
void CNavigatorDialog::mouseMoveEvent(QMouseEvent *event)
{
if (!handleMouseMoveEvent(event)) { QDialog::mouseMoveEvent(event); } ;
}
void CNavigatorDialog::mousePressEvent(QMouseEvent *event)
{
if (!handleMousePressEvent(event)) { QDialog::mousePressEvent(event); }
}
void CNavigatorDialog::ps_showContextMenu(const QPoint &pos)
{
QPoint globalPos = this->mapToGlobal(pos);
QScopedPointer<QMenu> contextMenu(new QMenu(this));
this->addToContextMenu(contextMenu.data());
QAction *selectedItem = contextMenu.data()->exec(globalPos);
Q_UNUSED(selectedItem);
}
void CNavigatorDialog::ps_changeLayout()
{
QAction *a = qobject_cast<QAction *>(QObject::sender());
if (!a) { return; }
QString v(a->data().toString());
if (v == "1c") { buildNavigator(1);}
else if (v == "2c") { buildNavigator(2);}
else if (v == "1r") { buildNavigator(columnsForRows(1));}
else if (v == "2r") { buildNavigator(columnsForRows(2));}
}
void CNavigatorDialog::insertOwnActions()
{
QIcon i(CIcons::changeIconBackgroundColor(this->style()->standardIcon(QStyle::SP_TitleBarCloseButton), Qt::white, QSize(16, 16)));
QAction *a = new QAction(i, "Close", this);
connect(a, &QAction::triggered, this, &CNavigatorDialog::close);
this->addAction(a);
}
int CNavigatorDialog::columnsForRows(int rows)
{
Q_ASSERT_X(rows >= 0, Q_FUNC_INFO, "no rows");
int items = this->actions().size();
int c = items / rows;
return (c * rows) < items ? c + 1 : c;
}
void CNavigatorDialog::addToContextMenu(QMenu *contextMenu) const
{
QAction *a;
a = contextMenu->addAction(CIcons::resize16(), "1 row", this, &CNavigatorDialog::ps_changeLayout);
a->setData("1r");
a = contextMenu->addAction(CIcons::resize16(), "2 rows", this, &CNavigatorDialog::ps_changeLayout);
a->setData("2r");
a = contextMenu->addAction(CIcons::resize16(), "1 column", this, &CNavigatorDialog::ps_changeLayout);
a->setData("1c");
a = contextMenu->addAction(CIcons::resize16(), "2 columns", this, &CNavigatorDialog::ps_changeLayout);
a->setData("2c");
contextMenu->addSeparator();
QString frameLessActionText = this->isFrameless() ? "Normal window" : "Frameless";
contextMenu->addAction(BlackMisc::CIcons::tableSheet16(), frameLessActionText, this, &CNavigatorDialog::toggleFrameless);
}
} // ns
} // ns

View File

@@ -0,0 +1,84 @@
/* Copyright (C) 2016
* swift project Community / Contributors
*
* This file is part of swift Project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKGUI_COMPONENTS_NAVIGATORDIALOG_H
#define BLACKGUI_COMPONENTS_NAVIGATORDIALOG_H
#include "blackgui/blackguiexport.h"
#include "blackgui/enableforframelesswindow.h"
#include <QDialog>
#include <QScopedPointer>
#include <QMenu>
namespace Ui { class CNavigatorDialog; }
namespace BlackGui
{
namespace Components
{
/*!
* Navigators dialog
*/
class BLACKGUI_EXPORT CNavigatorDialog :
public QDialog,
public CEnableForFramelessWindow
{
Q_OBJECT
public:
//! Constructor
CNavigatorDialog(QWidget *parent = nullptr);
//! Destructor
~CNavigatorDialog();
//! Navigator
void buildNavigator(int columns);
//! Toggle frameless mode
void toggleFrameless();
protected:
//! Style sheet has changed
void ps_onStyleSheetsChanged();
//! \copydoc QMainWindow::mouseMoveEvent
virtual void mouseMoveEvent(QMouseEvent *event) override;
//! \copydoc QMainWindow::mousePressEvent
virtual void mousePressEvent(QMouseEvent *event) override;
private slots:
//! Context menu
void ps_showContextMenu(const QPoint &pos);
//! Change the layout
void ps_changeLayout();
private:
//! Insert own actions
void insertOwnActions();
//! Contribute to menu
void addToContextMenu(QMenu *contextMenu) const;
//! How many columns for given rows
int columnsForRows(int rows);
QScopedPointer<Ui::CNavigatorDialog> ui;
bool m_firstBuild = true;
};
} // ns
} // ns
#endif // guard

View File

@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CNavigatorDialog</class>
<widget class="QDialog" name="CNavigatorDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>16</width>
<height>16</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
<property name="windowTitle">
<string>Navigator</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<property name="spacing">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QFrame" name="fr_NavigatorDialogInner">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="lineWidth">
<number>1</number>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>