File: qeditorfactory.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 (335 lines) | stat: -rw-r--r-- 7,627 bytes parent folder | download | duplicates (5)
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
/****************************************************************************
**
** 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 "qeditorfactory.h"

#ifdef _QSAFE_SHARED_SETTINGS_

/*!
	\file qeditorfactory.cpp
	\brief Implementation of the QEditorFactory class.
*/

#include "qcodeedit.h"

#include "qformatscheme.h"
#include "qlanguagefactory.h"
#include "qcodecompletionengine.h"

#include "qfoldpanel.h"
#include "qlinemarkpanel.h"
#include "qlinenumberpanel.h"
#include "qlinechangepanel.h"
#include "qgotolinepanel.h"
#include "qstatuspanel.h"
#include "qsearchreplacepanel.h"

#include "qeditor.h"
#include "qlinemarksinfocenter.h"

#include "qsettingsserver.h"

/*!
	\ingroup editor
	@{
	
	\class QEditorFactory
	\brief Convenience class that manages editors.
	
	QCodeEdit widgets are created through QEditorFactory using a simple
	QString representing a panel id. Each panel id is associated to a
	serialized panel layout (\see QPanelLayout::serialized() ).
	
	\see QCodeEdit
	\see QLanguageFactory
	\see QFormatScheme
*/

/*!
	\brief Construct a working editor factory
*/
QEditorFactory::QEditorFactory(QSettingsServer *s)
 :
#ifdef _QMDI_
	qmdiClientFactory(s),
#else
	QObject(s),
#endif
	QSettingsClient(s, "editor")
{
	m_defaultScheme = new QFormatScheme(QCE::fetchDataFile("formats.qxf"), this);
	
	QDocument::setFormatFactory(m_defaultScheme);
	
	m_languageFactory = new QLanguageFactory(m_defaultScheme, this);
	
	foreach ( QString dp, QCE::dataPathes() )
	{
		m_languageFactory->addDefinitionPath(dp);
	}
	
	if ( value("version").toInt() != 3 )
	{
		setValue("version", 3);
		
		// setup default layouts...
		beginGroup("layouts");
		
		setValue("default", "default");
		
		beginGroup("availables");
		
		beginGroup("empty");
		setValue("struct", QString());
		setValue("name", "No panels");
		endGroup();
		
		beginGroup("default");
		setValue("struct",
				QString::number(QCodeEdit::West)
				+ "{"
				+ Q_PANEL_ID(QLineMarkPanel)
				+ ","
				+ Q_PANEL_ID(QLineNumberPanel)
				+ ","
				+ Q_PANEL_ID(QFoldPanel)
				+ ","
				+ Q_PANEL_ID(QLineChangePanel)
				+ "}"
				
				+ QString::number(QCodeEdit::South)
				+ "{"
				+ Q_PANEL_ID(QStatusPanel)
				+ ","
				+ Q_PANEL_ID(QGotoLinePanel)
				+ ","
				+ Q_PANEL_ID(QSearchReplacePanel)
				+ "}"
				);
		
		setValue("name", "Default panel layout");
		endGroup();
		
		beginGroup("simple");
		setValue("struct",
				QString::number(QCodeEdit::West)
				+ "{"
				+ Q_PANEL_ID(QLineNumberPanel)
				+ ","
				+ Q_PANEL_ID(QFoldPanel)
				+ "}"
				
				+ QString::number(QCodeEdit::South)
				+ "{"
				+ Q_PANEL_ID(QStatusPanel)
				+ "}"
				);
		
		setValue("name", "Trimmed-down panel layout");
		endGroup();
		
		endGroup();
		
		endGroup();
	}
	
}

/*!
	\brief dtor
*/
QEditorFactory::~QEditorFactory()
{
	
}

/*!
	\reimp
	\brief Creates an editor with default layout for the given file
	\param filename file to load in the editor
	\return a managed QEditor object
*/
qmdiClient* QEditorFactory::createClient(const QString& filename) const
{
	return editor(filename, defaultLayout())->editor();
}

/*!
	\brief Create a managed editor
	\param f file to load
	\param layout panel layout to use
*/
QCodeEdit* QEditorFactory::editor(const QString& f, const QString& layout) const
{
	QCodeEdit *e = new QCodeEdit(layout.isEmpty() ? defaultLayout() : layout);
	
	//m_config->hookEditor(def, e);
	
	connect(e->editor()	, SIGNAL( loaded(QEditor*, QString) ),
			this		, SLOT  ( loaded(QEditor*, QString) ) );
	
	connect(e->editor()	, SIGNAL( saved(QEditor*, QString) ),
			this		, SLOT  ( saved(QEditor*, QString) ) );
	
	// set syntax handlers
	m_languageFactory->setLanguage(e->editor(), f);
	
	if ( f.count() && QFile::exists(f) )
	{
		// load contents
		e->editor()->load(f);
		
		// set line marks back...
		QLineMarksInfoCenter::instance()->flush(f);
	} else {
		e->editor()->setTitle(tr("untitled"));
		e->editor()->setContentModified(true);
	}
	
	return e;
}

/*!
	\overload
	\param f file to load
	\param l language definition to use
	\param s format scheme to use
	\param en code completion engine to use
	\param layout panel layout to use
*/
QCodeEdit* QEditorFactory::editor(	const QString& f,
									QLanguageDefinition *l,
									QFormatScheme *s,
									QCodeCompletionEngine *en,
									const QString& layout) const
{
	QCodeEdit *e = new QCodeEdit(layout.isEmpty() ? defaultLayout() : layout);
	
	//m_config->hookEditor(def, e);
	
	connect(e->editor()	, SIGNAL( loaded(QEditor*, QString) ),
			this		, SLOT  ( loaded(QEditor*, QString) ) );
	
	connect(e->editor()	, SIGNAL( saved(QEditor*, QString) ),
			this		, SLOT  ( saved(QEditor*, QString) ) );
	
	// set syntax handlers
	//m_languageFactory->setLanguage(e->editor(), l, en);
	e->editor()->setLanguageDefinition(l);
	e->editor()->document()->setFormatScheme(s ? s : m_defaultScheme);
	e->editor()->setCompletionEngine(en ? en->clone() : 0);
	
	if ( f.count() && QFile::exists(f) )
	{
		// load contents
		e->editor()->load(f);
		
		// set line marks back...
		QLineMarksInfoCenter::instance()->flush(f);
	} else {
		e->editor()->setTitle(tr("untitled"));
		e->editor()->setContentModified(true);
	}
	
	return e;
}

/*!
	\brief Called whenever a managed editor save its content
	
	Update the language definition/code completion engine if needed
	and emits the fileSaved signal
*/
void QEditorFactory::saved(QEditor *e, const QString& f)
{
	if ( !e || !e->document() )
		return;
	
	m_languageFactory->setLanguage(e, f);
	emit fileSaved(f);
}

/*!
	\brief Placeholder
*/
void QEditorFactory::loaded(QEditor *e, const QString& f)
{
	Q_UNUSED(f)
	
	if ( !e || !e->document() )
		return;
	
}

/*!
	\return The default panel layout
*/
QString QEditorFactory::defaultLayout() const
{
	QSettingsClient c(*this);
	
	c.beginGroup("layouts");
	QString a = c.value("default").toString();
	
	if ( a.isEmpty() )
	{
		c.beginGroup("availables");
		
		a = childGroups().at(0);
		
		c.endGroup();
	}
	
	c.endGroup();
	
	return layout(a);
}

/*!
	\return The layout structure associated with a given layout alias
*/
QString QEditorFactory::layout(const QString& alias) const
{
	return value("layouts/availables/" + alias + "/struct").toString();
}

/*!
	\brief Register a layout
	\param a layout alias
	\param layout layout structure
*/
void QEditorFactory::registerLayout(const QString& a, const QString& layout)
{
	beginGroup("layouts");
	beginGroup("availables");
	beginGroup(a);
	setValue("struct", layout);
	setValue("name", a);
	endGroup();
	endGroup();
	endGroup();
}

/*!
	\return an accessor to the settings associated with a given layout alias
*/
QSettingsClient QEditorFactory::settings(const QString& alias)
{
	return QSettingsClient(*this, "layouts/availables/" + alias);
}

/*! @} */

#endif