Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 6622b36

Browse files
committed
Merge pull request #19 from macgitver/feature/viewsmenu
Initial version of Views-Menu
2 parents 502adb7 + 46cb88c commit 6622b36

File tree

9 files changed

+374
-17
lines changed

9 files changed

+374
-17
lines changed

src/libHeaven/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ SET( SRC_FILES
6767
CentralUI/Views/View.cpp
6868
CentralUI/Views/AbstractViewWidget.cpp
6969
CentralUI/Views/ViewDescriptor.cpp
70+
CentralUI/Views/ViewDescriptorRegistrar.cpp
7071
CentralUI/Views/ViewIdentifier.cpp
7172

7273
CentralUI/Contexts/ContextKeys.cpp
@@ -202,6 +203,8 @@ SET( PRI_HDR_FILES
202203
CentralUI/States/WindowStateView.hpp
203204
CentralUI/States/WindowStateWindow.hpp
204205

206+
CentralUI/Views/ViewDescriptorRegistrar.hpp
207+
205208
Widgets/FooterWidget.hpp
206209
Widgets/ModeSwitchCombo.hpp
207210
Widgets/TabWidget.hpp
@@ -217,6 +220,7 @@ SET( RCC_FILES
217220
)
218221

219222
SET( HID_FILES
223+
CentralUI/Views/ViewsMergerActions.hid
220224
CentralUI/ContainerWidgets/MultiBarContainerWidgetActions.hid
221225
MultiBar/MultiBarViewWidgetActions.hid
222226
)

src/libHeaven/CentralUI/Views/View.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <QVBoxLayout>
1919

2020
#include "libHeaven/CentralUI/Views/View.hpp"
21+
#include "libHeaven/CentralUI/Views/ViewDescriptorRegistrar.hpp"
2122
#include "libHeaven/CentralUI/Contexts/ViewContextManager.hpp"
2223

2324
namespace Heaven
@@ -152,6 +153,8 @@ namespace Heaven
152153
*/
153154
void View::closeView()
154155
{
156+
ViewDescriptor::Registrar::self().viewClosed(this);
157+
155158
parentContainer()->take( this );
156159

157160
deleteLater();

src/libHeaven/CentralUI/Views/ViewDescriptor.cpp

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright (C) 2012-2013 The MacGitver-Developers <[email protected]>
44
*
55
* (C) Sascha Cunz <[email protected]>
6+
* (C) Cunz RaD Ltd
67
*
78
* This program is free software; you can redistribute it and/or modify it under the terms of the
89
* GNU General Public License (Version 2) as published by the Free Software Foundation.
@@ -16,7 +17,12 @@
1617
*
1718
*/
1819

20+
#include <QDebug>
21+
#include <QAction>
22+
23+
#include "libHeaven/CentralUI/Views/View.hpp"
1924
#include "libHeaven/CentralUI/Views/ViewDescriptor.hpp"
25+
#include "libHeaven/CentralUI/Views/ViewDescriptorRegistrar.hpp"
2026

2127
namespace Heaven
2228
{
@@ -26,7 +32,7 @@ namespace Heaven
2632
* @ingroup CentralUI
2733
* @brief A descriptor for available views
2834
*
29-
* This class stores meta data about the available views. It alos acts as a factory to create
35+
* This class stores meta data about the available views. It also acts as a factory to create
3036
* real View objects, when required.
3137
*
3238
* Each instance of this class represents one View.
@@ -51,14 +57,19 @@ namespace Heaven
5157
*/
5258
ViewDescriptor::ViewDescriptor( const ViewIdentifier& id, const QString& displayName,
5359
ViewDescriptor::CreatorFunc creator )
54-
: mIdentifier( id )
55-
, mDisplayName( displayName )
56-
, mCreatorFunc( creator )
60+
: mIdentifier(id)
61+
, mDisplayName(displayName)
62+
, mCreatorFunc(creator)
63+
, mCreatedView(NULL)
64+
, mActivatorAction(NULL)
5765
{
58-
mDescriptors.insert( id, this );
66+
Registrar::self().insert( id, this );
5967
}
6068

61-
QHash< ViewIdentifier, ViewDescriptor* > ViewDescriptor::mDescriptors;
69+
ViewDescriptor::~ViewDescriptor()
70+
{
71+
delete mActivatorAction;
72+
}
6273

6374
/**
6475
* @brief Get the display name for this descriptor
@@ -99,7 +110,7 @@ namespace Heaven
99110
*/
100111
void ViewDescriptor::unregister()
101112
{
102-
mDescriptors.remove( mIdentifier );
113+
Registrar::self().remove(mIdentifier);
103114
delete this;
104115
}
105116

@@ -113,7 +124,7 @@ namespace Heaven
113124
*/
114125
ViewDescriptor* ViewDescriptor::get( const ViewIdentifier& id )
115126
{
116-
return mDescriptors.value( id, NULL );
127+
return Registrar::self().get(id);
117128
}
118129

119130
/**
@@ -126,8 +137,68 @@ namespace Heaven
126137
*/
127138
View* ViewDescriptor::createView() const
128139
{
129-
return mCreatorFunc();
140+
View* view = mCreatorFunc();
141+
if (!view) {
142+
return NULL;
143+
}
144+
145+
mCreatedView = view;
146+
147+
Registrar::self().viewOpened(view);
148+
149+
return view;
130150
}
131151

152+
/**
153+
* @brief Get the activator action of this view
154+
*
155+
* @return A QAction that can be used to open/close this view
156+
*/
157+
QAction* ViewDescriptor::activatorAction()
158+
{
159+
if (!mActivatorAction) {
160+
createActivatorAction();
161+
}
162+
163+
return mActivatorAction;
164+
}
165+
166+
DynamicActionMerger* ViewDescriptor::actionMerger()
167+
{
168+
return Registrar::self().actionMerger();
169+
}
170+
171+
void ViewDescriptor::createActivatorAction()
172+
{
173+
if (mActivatorAction) {
174+
return;
175+
}
176+
177+
mActivatorAction = new QAction(mDisplayName, &Registrar::self());
178+
mActivatorAction->setCheckable(true);
179+
180+
connect(mActivatorAction, SIGNAL(triggered(bool)),
181+
this, SLOT(onActivatorAction(bool)));
182+
}
183+
184+
void ViewDescriptor::mergeViewsMenu(const QByteArray& mergePlace)
185+
{
186+
ViewDescriptor::Registrar::self().acViewsMergerAC->mergeInto(mergePlace);
187+
}
188+
189+
void ViewDescriptor::onActivatorAction(bool triggered)
190+
{
191+
if (triggered && !mCreatedView) {
192+
qDebug() << "Should open:" << mIdentifier;
193+
}
194+
else if (!triggered && mCreatedView) {
195+
mCreatedView->closeView();
196+
}
197+
}
198+
199+
void ViewDescriptor::setViewClosed()
200+
{
201+
mCreatedView = NULL;
202+
}
132203

133204
}

src/libHeaven/CentralUI/Views/ViewDescriptor.hpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,34 @@
1919
#ifndef HEAVEN_VIEW_DESCRIPTOR_HPP
2020
#define HEAVEN_VIEW_DESCRIPTOR_HPP
2121

22+
#include <QObject>
23+
2224
#include "libHeaven/HeavenApi.hpp"
2325

2426
#include "libHeaven/CentralUI/Views/ViewIdentifier.hpp"
2527

28+
class QAction;
29+
2630
namespace Heaven
2731
{
2832

33+
class DynamicActionMerger;
2934
class View;
3035

31-
class HEAVEN_API ViewDescriptor
36+
class HEAVEN_API ViewDescriptor : public QObject
3237
{
38+
Q_OBJECT
3339
public:
40+
class Registrar;
41+
friend class Registrar;
3442
typedef View* (*CreatorFunc)( );
3543

3644
public:
37-
ViewDescriptor( const ViewIdentifier& id, const QString& displayName, CreatorFunc creator );
45+
ViewDescriptor(const ViewIdentifier& id, const QString& displayName, CreatorFunc creator);
46+
~ViewDescriptor();
3847

3948
public:
49+
QAction* activatorAction();
4050
QString displayName() const;
4151
ViewIdentifier identifier() const;
4252
ViewDescriptor::CreatorFunc creatorFunc() const;
@@ -45,13 +55,23 @@ namespace Heaven
4555
View* createView() const;
4656

4757
public:
58+
static DynamicActionMerger* actionMerger();
59+
static void mergeViewsMenu(const QByteArray& mergePlace);
4860
static ViewDescriptor* get( const ViewIdentifier& id );
4961

62+
private:
63+
void setViewClosed();
64+
void createActivatorAction();
65+
66+
private slots:
67+
void onActivatorAction(bool triggered);
68+
5069
private:
5170
const ViewIdentifier mIdentifier;
5271
const QString mDisplayName;
5372
const CreatorFunc mCreatorFunc;
54-
static QHash< ViewIdentifier, ViewDescriptor* > mDescriptors;
73+
mutable View* mCreatedView;
74+
QAction* mActivatorAction;
5575
};
5676

5777
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* libHeaven - A Qt-based ui framework for strongly modularized applications
3+
* Copyright (C) 2012-2013 The MacGitver-Developers <[email protected]>
4+
*
5+
* (C) Sascha Cunz <[email protected]>
6+
* (C) Cunz RaD Ltd.
7+
*
8+
* This program is free software; you can redistribute it and/or modify it under the terms of the
9+
* GNU General Public License (Version 2) as published by the Free Software Foundation.
10+
*
11+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
12+
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License along with this program; if
16+
* not, see <http://www.gnu.org/licenses/>.
17+
*
18+
*/
19+
20+
#include <QAction>
21+
22+
#include "libHeaven/Actions/DynamicActionMerger.hpp"
23+
24+
#include "View.hpp"
25+
#include "ViewDescriptorRegistrar.hpp"
26+
27+
namespace Heaven
28+
{
29+
30+
/**
31+
* @internal
32+
* @class ViewDescriptor::Registrar
33+
*
34+
* @brief Internal class to manage registered ViewDescriptors and the Views-Menu
35+
*
36+
*/
37+
38+
ViewDescriptor::Registrar::Registrar()
39+
{
40+
setupActions(this);
41+
damViewsMerger->setMode(DAMergerAdvancedList);
42+
sSelf = this;
43+
}
44+
45+
ViewDescriptor::Registrar& ViewDescriptor::Registrar::self()
46+
{
47+
if (sSelf == NULL) {
48+
new Registrar();
49+
}
50+
return *sSelf;
51+
}
52+
53+
ViewDescriptor::Registrar* ViewDescriptor::Registrar::sSelf = NULL;
54+
55+
void ViewDescriptor::Registrar::insert(const ViewIdentifier& id, ViewDescriptor* vd)
56+
{
57+
mDescriptors.insert(id, vd);
58+
rebuildMerger();
59+
}
60+
61+
void ViewDescriptor::Registrar::remove(const ViewIdentifier& id)
62+
{
63+
mDescriptors.remove(id);
64+
rebuildMerger();
65+
}
66+
67+
ViewDescriptor* ViewDescriptor::Registrar::get(const ViewIdentifier& id) const
68+
{
69+
return mDescriptors.value(id, NULL);
70+
}
71+
72+
void ViewDescriptor::Registrar::viewClosed(View* view)
73+
{
74+
ViewIdentifier id = view->identifier();
75+
ViewDescriptor* vd = get(id);
76+
77+
if (vd) {
78+
QAction* aa = vd->activatorAction();
79+
aa->setChecked(false);
80+
81+
vd->setViewClosed();
82+
}
83+
}
84+
85+
void ViewDescriptor::Registrar::viewOpened(View* view)
86+
{
87+
ViewIdentifier id = view->identifier();
88+
ViewDescriptor* vd = get(id);
89+
90+
if (vd) {
91+
QAction* aa = vd->activatorAction();
92+
aa->setChecked(true);
93+
}
94+
}
95+
96+
DynamicActionMerger* ViewDescriptor::Registrar::actionMerger()
97+
{
98+
return damViewsMerger;
99+
}
100+
101+
void ViewDescriptor::Registrar::rebuildMerger()
102+
{
103+
Q_ASSERT(damViewsMerger);
104+
damViewsMerger->clear();
105+
106+
QMap<ViewIdentifier, ViewDescriptor*> descriptors;
107+
108+
foreach (ViewIdentifier id, mDescriptors.keys()) {
109+
descriptors.insert(id, mDescriptors[id]);
110+
}
111+
112+
foreach (ViewIdentifier id, descriptors.keys()) {
113+
ViewDescriptor* vd = get(id);
114+
if (vd) {
115+
damViewsMerger->addAction(vd->activatorAction());
116+
}
117+
}
118+
}
119+
120+
}

0 commit comments

Comments
 (0)