1 //=============================================================================
2 //
3 //   File : KvsObject_dockWindow.cpp
4 //   Creation date : Thu 29 Dec 2005 23:45:11 by Szymon Stefanek
5 //
6 //   This file is part of the KVIrc IRC Client distribution
7 //   Copyright (C) 2005-2010 Szymon Stefanek <pragma at kvirc dot net>
8 //
9 //   This program is FREE software. You can redistribute it and/or
10 //   modify it under the terms of the GNU General Public License
11 //   as published by the Free Software Foundation; either version 2
12 //   of the License, or (at your option) any later version.
13 //
14 //   This program is distributed in the HOPE that it will be USEFUL,
15 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
16 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 //   See the GNU General Public License for more details.
18 //
19 //   You should have received a copy of the GNU General Public License
20 //   along with this program. If not, write to the Free Software Foundation,
21 //   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 //=============================================================================
24 
25 #include "KvsObject_dockWindow.h"
26 
27 #include "KviMainWindow.h"
28 #include "KviLocale.h"
29 #include "KviQString.h"
30 
31 #include <QDockWidget>
32 #define QT_DOCK_WINDOW QDockWidget
33 #include <QLayout>
34 
35 /*
36 	@doc: dockwindow
37 	@title:
38 		dockwindow class
39 	@type:
40 		class
41 	@short:
42 		A window dockable to the KVIrc main frame
43 	@inherits:
44 		[class]object[/class]
45 		[class]widget[/class]
46 	@description:
47 		A window dockable to the KVIrc main frame borders (like a toolbar).
48 	@functions:
49 		!fn: $addWidget(<widget:hobject>)
50 		Adds <widget> as the main widget inside this dock window.[br]
51 		The widget must be a child of this dock window (otherwise strange things may happen).
52 		In order to use a layout, apply it to <widget>, as shown in the example.
53 		!fn: $setAllowedDockAreas(<docks:string>)
54 		Sets the allowed main window dock areas for this dock window.[br]
55 		<docks> must be a combination of [i]l[/i], [i]r[/i], [i]t[/i], [i]b[/i], [b]f[/b] and [b]m[/b].[br]
56 		[b]l[/b] stands for left dock area, [b]r[/b] stands for right dock area, [b]t[/b] stands for the top dock areas, [b]b[/b] stands for the bottom dock area,
57 		[b]f[/b] stands for [i]floating[/i] and [b]m[/b] for [i]minimized[/i].[br]
58 		If a flag is present then the related block area is enabled,otherwise it is disabled.
59 		!fn: $dock(<dockarea:string>)
60 		Docks this dock window to the specified dockarea of the main KVIrc window which can be one of "l" (left dock area), "t" (top dock area), "r" (right dock area), "b" (bottom dock area), "f" (floating) and "m" (minimized).
61 	@examples:
62 		[example]
63 			%dock = $new(dockwindow)
64 			%dock->$setWindowTitle("This is the dock window title")
65 			%dock->$setAllowedDockAreas("l","f")
66 
67 			%widget = $new(widget, %dock)
68 			%dock->$addWidget(%widget)
69 
70 			%box=$new(vbox,%widget)
71 			%layout=$new(layout,%widget)
72 			%layout->$addWidget(%box,0,0)
73 
74 			%label = $new(label,%box)
75 			%label->$setText("This is a text label")
76 
77 			%lineedit = $new(lineedit,%box)
78 			%lineedit->$setText("This is a lineedit")
79 
80 			%button = $new(button, %box)
81 			%button->$setText("Close me")
82 
83 			privateimpl(%dock,closeMe)
84 			{
85 				delete $$
86 			}
87 			objects.connect  %button clicked %dock closeMe
88 
89 			%dock->$show()
90 		[/example]
91 */
92 
93 KVSO_BEGIN_REGISTERCLASS(KvsObject_dockWindow, "dockwindow", "widget")
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_dockWindow,addWidget)94 KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_dockWindow, addWidget)
95 KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_dockWindow, setAllowedDockAreas)
96 KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_dockWindow, dock)
97 KVSO_END_REGISTERCLASS(KvsObject_dockWindow)
98 
99 KVSO_BEGIN_CONSTRUCTOR(KvsObject_dockWindow, KvsObject_widget)
100 KVSO_END_CONSTRUCTOR(KvsObject_dockWindow)
101 
102 KVSO_BEGIN_DESTRUCTOR(KvsObject_dockWindow)
103 KVSO_END_DESTRUCTOR(KvsObject_dockWindow)
104 
105 #define _pDockWindow ((QDockWidget *)widget())
106 
107 bool KvsObject_dockWindow::init(KviKvsRunTimeContext *, KviKvsVariantList *)
108 {
109 	QDockWidget * pWidget = new QDockWidget(g_pMainWindow);
110 	pWidget->setObjectName(getName());
111 	setObject(pWidget);
112 	return true;
113 }
114 
KVSO_CLASS_FUNCTION(dockWindow,addWidget)115 KVSO_CLASS_FUNCTION(dockWindow, addWidget)
116 {
117 	CHECK_INTERNAL_POINTER(widget())
118 	kvs_hobject_t hWidget;
119 	KVSO_PARAMETERS_BEGIN(c)
120 	KVSO_PARAMETER("widget", KVS_PT_HOBJECT, 0, hWidget)
121 	KVSO_PARAMETERS_END(c)
122 	if(hWidget == (kvs_hobject_t) nullptr)
123 	{
124 		// null widget ?
125 		c->warning(__tr2qs_ctx("Can't add a null object", "objects"));
126 		return true;
127 	}
128 
129 	KviKvsObject * pWidget = KviKvsKernel::instance()->objectController()->lookupObject(hWidget);
130 	if(!pWidget)
131 	{
132 		c->warning(__tr2qs_ctx("Invalid object handle passed as parameter (the object no longer exists?)", "objects"));
133 		return true;
134 	}
135 
136 	if(!pWidget->object())
137 	{
138 		c->warning(__tr2qs_ctx("Object in invalid state", "objects"));
139 		return true;
140 	}
141 
142 	if(!pWidget->object()->isWidgetType())
143 	{
144 		c->warning(__tr2qs_ctx("Can't set a non-widget object to be the main widget of a dock window", "objects"));
145 		return true;
146 	}
147 
148 	if(((QWidget *)(pWidget->object()))->parent() != (QObject *)_pDockWindow)
149 	{
150 		c->warning(__tr2qs_ctx("The added widget is not a child of this dock window", "objects"));
151 	}
152 
153 	_pDockWindow->setWidget((QWidget *)(pWidget->object()));
154 	return true;
155 }
156 
KVSO_CLASS_FUNCTION(dockWindow,setAllowedDockAreas)157 KVSO_CLASS_FUNCTION(dockWindow, setAllowedDockAreas)
158 {
159 	CHECK_INTERNAL_POINTER(widget())
160 	QString szFlags;
161 	KVSO_PARAMETERS_BEGIN(c)
162 	KVSO_PARAMETER("docks", KVS_PT_STRING, 0, szFlags)
163 	KVSO_PARAMETERS_END(c)
164 	Qt::DockWidgetAreas fAreas = Qt::NoDockWidgetArea;
165 	if(szFlags.indexOf('t', Qt::CaseInsensitive) >= 0)
166 		fAreas |= Qt::TopDockWidgetArea;
167 	if(szFlags.indexOf('l', Qt::CaseInsensitive) >= 0)
168 		fAreas |= Qt::LeftDockWidgetArea;
169 	if(szFlags.indexOf('r', Qt::CaseInsensitive) >= 0)
170 		fAreas |= Qt::RightDockWidgetArea;
171 	if(szFlags.indexOf('b', Qt::CaseInsensitive) >= 0)
172 		fAreas |= Qt::BottomDockWidgetArea;
173 	_pDockWindow->setAllowedAreas(fAreas);
174 	QDockWidget::DockWidgetFeatures fFeatures = _pDockWindow->features();
175 	if(szFlags.indexOf('f', Qt::CaseInsensitive))
176 		fFeatures |= QDockWidget::DockWidgetFloatable;
177 	else
178 		fFeatures &= ~QDockWidget::DockWidgetFloatable;
179 	// no support for minimized dock widgets
180 	_pDockWindow->setFeatures(fFeatures);
181 
182 	return true;
183 }
184 
KVSO_CLASS_FUNCTION(dockWindow,dock)185 KVSO_CLASS_FUNCTION(dockWindow, dock)
186 {
187 	CHECK_INTERNAL_POINTER(widget())
188 	QString szDock;
189 	KVSO_PARAMETERS_BEGIN(c)
190 	KVSO_PARAMETER("dock", KVS_PT_STRING, 0, szDock)
191 	KVSO_PARAMETERS_END(c)
192 	g_pMainWindow->removeDockWidget(_pDockWindow);
193 	if(szDock.indexOf('m', Qt::CaseInsensitive) == -1)
194 		_pDockWindow->setFloating(false);
195 	if(szDock.indexOf('t', Qt::CaseInsensitive) != -1)
196 		g_pMainWindow->addDockWidget(Qt::TopDockWidgetArea, _pDockWindow);
197 	else if(szDock.indexOf('l', Qt::CaseInsensitive) != -1)
198 		g_pMainWindow->addDockWidget(Qt::LeftDockWidgetArea, _pDockWindow);
199 	else if(szDock.indexOf('r', Qt::CaseInsensitive) != -1)
200 		g_pMainWindow->addDockWidget(Qt::RightDockWidgetArea, _pDockWindow);
201 	else if(szDock.indexOf('b', Qt::CaseInsensitive) != -1)
202 		g_pMainWindow->addDockWidget(Qt::BottomDockWidgetArea, _pDockWindow);
203 	else if(szDock.indexOf('f', Qt::CaseInsensitive) != -1)
204 		_pDockWindow->setFloating(true);
205 	else if(szDock.indexOf('m', Qt::CaseInsensitive) != -1)
206 		qDebug("Sorry: no support for minimized dock widgets in Qt4");
207 	else
208 		c->warning(__tr2qs_ctx("Invalid dock area specified", "objects"));
209 	return true;
210 }
211