File: qcodeedit.cpp

package info (click to toggle)
texstudio 2.11.2%2Bdebian-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 41,292 kB
  • ctags: 12,405
  • sloc: cpp: 93,072; xml: 10,217; ansic: 4,153; sh: 145; makefile: 56
file content (407 lines) | stat: -rw-r--r-- 9,049 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/****************************************************************************
**
** Copyright (C) 2006-2009 fullmetalcoder <fullmetalcoder@hotmail.fr>
**
** This file is part of the Edyuk project <http://edyuk.org>
** 
** This file may be used under the terms of the GNU General Public License
** version 3 as published by the Free Software Foundation and appearing in the
** file GPL.txt included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "qcodeedit.h"

/*!
	\file qcodeedit.cpp
	\brief Implementation of the QCodeEdit class
*/

#include "qpanel.h"
#include "qeditor.h"
#include "qpanellayout.h"
#include "qmetautils.h"

/*!
	\class QCodeEdit
	\brief A thin layer over QEditor
	
	The QCodeEdit class provides simple means to associate panels with editors and manage them.
*/

/*!
	\internal
	\class QPanelWatcher
	
	A class designed to work around some limitations of the hide/show event system and allow
	proper setting and conservation of default visibility for panels.
*/
class QPanelWatcher : public QObject
{
	public:
		QPanelWatcher(QCodeEdit *e)
		 : qce(e)
		{
		
		}
		
		bool eventFilter(QObject *o, QEvent *e)
		{
			QPanel *p = qobject_cast<QPanel*>(o);
			QAction *a = qce->toggleViewAction(p);
			
			if ( a )
			{
				bool sig = a->signalsBlocked();
				a->blockSignals(true);
				
				if ( a->isChecked() && e->type() == QEvent::Hide )
					a->setChecked(false);
				else if ( !a->isChecked() && e->type() == QEvent::Show )
					a->setChecked(true);
				
				a->blockSignals(sig);
			}
			
			return QObject::eventFilter(o, e);
		}
		
	private:
		QCodeEdit *qce;
};

QStringList __qce_data_path;

/*!
	\brief Centralized access point to data fetching
*/
QString QCE::fetchDataFile(const QString& file)
{
	if ( QFileInfo(file).isAbsolute() )
		return file;
	
	foreach ( const QString& dp, __qce_data_path )
	{
		QDir d(dp);
		
		if ( d.exists(file) )
			return d.absoluteFilePath(file);
	}
	
	return file;
}

/*!
	\return The list of pathes used by QCE to fetch data
*/
QStringList QCE::dataPathes()
{
	return __qce_data_path;
}

/*!
	\brief Add a path to the list of pathes used to fetch data
*/
void QCE::addDataPath(const QString& path)
{
	if ( !__qce_data_path.contains(path) )
		__qce_data_path << path;
}

QList<QCodeEdit*> QCodeEdit::m_instances;

/*!
	\brief ctor
	
	The created editor object comes with builtin actions.
*/
QCodeEdit::QCodeEdit(QWidget *p)
 : m_panelsMenu(0)
{
	m_editor = new QEditor(p);
	m_watcher = new QPanelWatcher(this);
	m_layout = new QPanelLayout(m_editor);
	
	m_instances << this;
}

/*!
	\brief ctor
	\param actions whether the QEditor object should create builtin actions
*/
QCodeEdit::QCodeEdit(bool actions, QWidget *p, QDocument *doc)
 : m_panelsMenu(0)
{
	m_editor = new QEditor(actions, p,doc);
	m_watcher = new QPanelWatcher(this);
	m_layout = new QPanelLayout(m_editor);
	
	m_instances << this;
}

/*!
	\brief ctor
	\param layout structure of the panel layout
	
	The created editor object comes with builtin actions.
*/
QCodeEdit::QCodeEdit(const QString& layout, QWidget *p)
 : m_panelsMenu(0)
{
	m_editor = new QEditor(p);
	m_watcher = new QPanelWatcher(this);
	m_layout = new QPanelLayout(layout, m_editor);
	
	m_instances << this;
}

/*!
	\brief ctor
	\param layout structure of the panel layout
	\param actions whether the QEditor object should create builtin actions
*/
QCodeEdit::QCodeEdit(const QString& layout, bool actions, QWidget *p)
 : m_panelsMenu(0)
{
	m_editor = new QEditor(actions, p);
	m_watcher = new QPanelWatcher(this);
	m_layout = new QPanelLayout(layout, m_editor);
	
	m_instances << this;
}

/*!
	\brief ctor
	\param e editor to manage
	\param p panel layout to associate with the editor
*/
QCodeEdit::QCodeEdit(QEditor *e, QPanelLayout *p)
 : m_panelsMenu(0)
{
	m_editor = e;
	m_watcher = new QPanelWatcher(this);
	m_layout = p ? p : new QPanelLayout(m_editor);
	
	m_instances << this;
}

/*!
	\brief ctor
	\param e editor to manage
	\param l structure of the panel layout
*/
QCodeEdit::QCodeEdit(QEditor *e, const QString& l)
 : m_panelsMenu(0)
{
	m_editor = e;
	m_watcher = new QPanelWatcher(this);
	m_layout = new QPanelLayout(l, m_editor);
	
	m_instances << this;
}

/*!
	\brief dtor
	
	\warning Destroyes the editor and the panel layout it manages
*/
QCodeEdit::~QCodeEdit()
{
	m_instances.removeAll(this);
	
	delete m_watcher;
	delete m_editor;
	delete m_layout;
}

/*!
	\return the managed editor
*/
QEditor* QCodeEdit::editor() const
{
	return m_editor;
}

/*!
	\return the panel layout associated with the managed editor
*/
QPanelLayout* QCodeEdit::panelLayout() const
{
	return m_layout;
}

/*!
	\brief Add a panel
	\return Toggle view action for the added panel
	\param panel panel to add
	\param pos position of the panel in the layout
	\param _add whether to add the show action of the panel to the menu of the editor
*/
QAction* QCodeEdit::addPanel(QPanel *panel, Position pos, bool _add)
{
	panel->attach(m_editor);
	
	QAction *a = new QAction(panel->type(), m_editor);
	a->setCheckable(true);
	a->setChecked(panel->defaultVisibility());
	
	QObject::connect(a		, SIGNAL( toggled(bool) ),
					panel	, SLOT  ( setVisible(bool) ) );
	
	m_layout->addWidget(panel, QPanelLayout::Position(pos));
	m_layout->update();
	
	m_actions << a;
	
	panel->installEventFilter(m_watcher);
	
	if ( _add )
	{
		if ( !m_panelsMenu )
		{
			m_panelsMenu = new QMenu(QEditor::tr("Panels"), m_editor);
			m_panelsMenu->menuAction()->setObjectName("panels");
			m_editor->addAction(m_panelsMenu->menuAction(), QEditor::tr("&View"), QString());
		}
		
		m_panelsMenu->addAction(a);
	}

	return a;
}

/*!
	\overload
	\return Toggle view action for the added panel
	\param name name of panel to add
	\param pos position of the panel in the layout
	\param _add whether to add the show action of the panel to the menu of the editor
*/
QAction* QCodeEdit::addPanel(const QString& name, Position pos, bool _add)
{
	return addPanel(QPanel::panel(name, m_editor), pos, _add);
}

/*!
	\return whether the editor has a panel of the given \a type
*/
bool QCodeEdit::hasPanel(const QString& type) const
{
	if ( !m_layout )
		return false;
	
	QList<QPanel*> l = m_layout->panels();
	
	foreach ( QPanel *p, l )
		if ( p->type() == type )
			return true;
	
	return false;
}

/*!
	\return a list of panels added to the editor
	\param type Type of panel to look for (no filtering is performed if empty)
*/
QList<QPanel*> QCodeEdit::panels(const QString& type) const
{
	if ( !m_layout )
		return QList<QPanel*>();
	
	QList<QPanel*> l = m_layout->panels();
	
	if ( type.isEmpty() )
		return l;
	
	int i = 0;
	
	while ( i < l.count() )
	{
		if ( l.at(i)->type() == type )
		{
			++i;
		} else {
			l.removeAt(i);
		}
	}
	
	return l;
}

/*!
	\return the toggle view action of a given panel
*/
QAction* QCodeEdit::toggleViewAction(QPanel *p) const
{
	int idx = panels().indexOf(p);
	return idx == -1 ? 0 : m_actions.at(idx);
}

/*!
	\brief Send a command to every panel of a given type
	\param signature method name suitable for QMetaObject::invokeMethod()
	\param args list of arguments suitable for QMetaObject::invokeMethod()
	
	Example use :
	\code
	sendPanelCommand("Status", "setVisible" Q_COMMAND << Q_ARG(bool, false));
	\endcode
*/
void QCodeEdit::sendPanelCommand(	const QString& type,
									const char *signature,
									const QList<QGenericArgument>& args)
{
	QList<QPanel*> lp = panels();
	
	//qDebug("looking for panel of type %s", qPrintable(type));
	
	foreach ( QPanel *p, lp )
	{
		if ( p && (p->type() == type) )
			QMetaObjectInvokeMethod(p, signature, args);
	}
}

void QCodeEdit::sendPanelCommand(	const QString& type,
						const char *signature,
						const QList<QVariant>& args){

	QList<QPanel*> lp = panels();

	//qDebug("looking for panel of type %s", qPrintable(type));

	foreach ( QPanel *p, lp )
	{
		if ( p && (p->type() == type) )
			QMetaObjectInvokeMethod(p, signature, args);
	}
}


/*!
	\return The QCodeEdit object managing a given editor or a null point if the given editor is unmanaged
*/
QCodeEdit* QCodeEdit::manager(QEditor *e)
{
	foreach ( QCodeEdit *m, m_instances )
		if ( m->m_editor == e )
			return m;
	
	return 0;
}

/*!
	\brief The (first) managed editor editing a given file or a null pointer if none found
*/
QEditor* QCodeEdit::managed(const QString& f)
{
	foreach ( QCodeEdit *m, m_instances )
		if ( m && m->m_editor && (m->m_editor->fileName() == f) )
			return m->m_editor;
	
	return 0;
}