File: qformatscheme.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 (574 lines) | stat: -rw-r--r-- 14,548 bytes parent folder | download
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/****************************************************************************
**
** 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 "mostQtHeaders.h"

#include "qformatscheme.h"

/*!
	\file qformatscheme.cpp
	\brief Implementation of QFormatScheme
	
	\see QFormatScheme
*/

/*!
	\ingroup language
	@{
	
	\class QFormatScheme
	\brief A storage/configuration class for shared highlighting formats
	
	It stores text formats used by highlighters interfaces and provides
	a default serializing format in QXF format (XML-based).
	
	\see QLanguageFactory
	\see QHighlighter
*/

#include "qformat.h"

#include <QDomText>
#include <QDomElement>
#include <QDomDocument>

#define QFORMAT_VERSION "1.0"

static bool bool_cast(const QString& s)
{
	return !QString::compare(s, QLatin1String("true")) || s.toUInt() == 1;
}

static void setFormatOption(QFormat& fmt, const QString& field, const QString& value){
	if ( field == "priority" )
		fmt.setPriority(value.toInt());
	else if ( field == "bold" )
		fmt.weight = bool_cast(value) ? QFont::Bold : QFont::Normal;
	else if ( field == "italic" )
		fmt.italic = bool_cast(value);
	else if ( field == "overline" )
		fmt.overline = bool_cast(value);
	else if ( field == "underline" )
		fmt.underline = bool_cast(value);
	else if ( field == "strikeout" )
		fmt.strikeout = bool_cast(value);
	else if ( field == "waveUnderline" )
		fmt.waveUnderline = bool_cast(value);
	else if ( field == "color" || field == "foreground" )
		fmt.foreground = QColor(value);
	else if ( field == "background" )
		fmt.background = QColor(value);
	else if ( field == "linescolor" )
		fmt.linescolor = QColor(value);
	else if ( field == "fontFamily" )
		fmt.fontFamily = value;
	else if ( field == "pointSize" )
		fmt.pointSize = value.toInt();
    else if ( field == "wrapAround" )
        fmt.wrapAround = bool_cast(value);
}

/*!
	\brief Constructor
*/
QFormatScheme::QFormatScheme(QObject *p)
 : QObject(p),modified(false)
{
	setFormat("normal", QFormat());
}

/*!
	\brief Constructor
	\param f Filename of a format settings file to load
*/
QFormatScheme::QFormatScheme(const QString& f, QObject *p)
 : QObject(p)
{
	load(f);
}

/*!
	\brief Destructor
*/
QFormatScheme::~QFormatScheme()
{
}

/*!
	\brief Re-initialize the format scheme
	
	Calling this method leaves the format scheme with only one
	format : the "normal" one, set to a default-constructed QFormat
*/
void QFormatScheme::clear()
{
	m_formatKeys.clear();
	m_formatValues.clear();
	
	setFormat("normal", QFormat());
}

/*!
	\brief Load format settings from a file
	\param f file to load data from

	The default implementation loads data in QXF format (XML-based)

	\note Previous content is discarded
*/
void QFormatScheme::load(const QString& f)
{
	clear();
	m_settings = f;

	QFile settings(f);
	
	if ( settings.open(QFile::ReadOnly | QFile::Text) )
	{
		QDomDocument doc;
		doc.setContent(&settings);
		
		load(doc.documentElement());
	}
}

/*!
	\brief Save the format settings to a file
	\param f target file (if none specified, last value passed to load is used)

	The default implementation saves data in QXF format (XML-based)
*/
void QFormatScheme::save(const QString& f) const
{
	QFile settings(f.count() ? f : m_settings);
	
	if ( settings.open(QFile::WriteOnly | QFile::Text) )
	{
		QDomDocument doc("QXF");
		QDomElement root = doc.createElement("QXF");
		save(root);
		doc.appendChild(root);
		settings.write(doc.toByteArray(4));
	}
}

/*!
	\overload
	\param elem Source element to scan
	\param ignoreNewIds whether unknown format identifiers should be ignored
	
	The given dom element must contain a proper version attribute and format
	data as child elements (&lt;format&gt; tags)
	
	\note Previous content is not discarded
*/
void QFormatScheme::load(const QDomElement& elem, bool ignoreNewIds)
{
	if (!elem.hasAttributes() && !elem.hasChildNodes()) return;

	if ( elem.attribute("version") < QFORMAT_VERSION )
	{
		qWarning("Format encoding version mismatch : [found]%s != [expected]%s",
				qPrintable(elem.attribute("version")),
				QFORMAT_VERSION);
		
		return;
	}
	
	QDomElement e, c;
	QDomNodeList l, f = elem.elementsByTagName("format");
	
	for ( int i = 0; i < f.count(); i++ )
	{
		e = f.at(i).toElement();
		
		if ( ignoreNewIds && !m_formatKeys.contains(e.attribute("id")) )
			continue;
		
		l = e.childNodes();
		
		QFormat fmt;
		
		for ( int i = 0; i < l.count(); i++ )
		{
			c = l.at(i).toElement();
			
			if ( c.isNull() )
				continue;
			
			QString field = c.tagName(),
					value = c.firstChild().toText().data();
			setFormatOption(fmt,field,value);
		}
		fmt.setPriority(fmt.priority); //update priority if other values changed

		setFormat(e.attribute("id"), fmt);
	}
}

/*!
	\overload
*/
void QFormatScheme::save(QDomElement& elem) const
{
	QDomDocument doc = elem.ownerDocument();
	elem.setAttribute("version", QFORMAT_VERSION);
	
	for ( int i = 0; i < m_formatKeys.count(); ++i )
	{
		QDomText t;
		QDomElement f, c = doc.createElement("format");
		
		c.setAttribute("id", m_formatKeys.at(i));
		
		const QFormat& fmt = m_formatValues.at(i);
		
		f = doc.createElement("priority");
		t = doc.createTextNode(QString::number(fmt.priority));
		f.appendChild(t);
		c.appendChild(f);

		f = doc.createElement("bold");
		t = doc.createTextNode((fmt.weight == QFont::Bold) ? "true" : "false");
		f.appendChild(t);
		c.appendChild(f);
		
		f = doc.createElement("italic");
		t = doc.createTextNode(fmt.italic ? "true" : "false");
		f.appendChild(t);
		c.appendChild(f);
		
		f = doc.createElement("overline");
		t = doc.createTextNode(fmt.overline ? "true" : "false");
		f.appendChild(t);
		c.appendChild(f);
		
		f = doc.createElement("underline");
		t = doc.createTextNode(fmt.underline ? "true" : "false");
		f.appendChild(t);
		c.appendChild(f);
		
		f = doc.createElement("strikeout");
		t = doc.createTextNode(fmt.strikeout ? "true" : "false");
		f.appendChild(t);
		c.appendChild(f);
		
		f = doc.createElement("waveUnderline");
		t = doc.createTextNode(fmt.waveUnderline ? "true" : "false");
		f.appendChild(t);
		c.appendChild(f);
		
		if ( fmt.foreground.isValid() )
		{
			f = doc.createElement("foreground");
			t = doc.createTextNode(fmt.foreground.name());
			f.appendChild(t);
			c.appendChild(f);
		}
		
		if ( fmt.background.isValid() )
		{
			f = doc.createElement("background");
			t = doc.createTextNode(fmt.background.name());
			f.appendChild(t);
			c.appendChild(f);
		}
		
		if ( fmt.linescolor.isValid() )
		{
			f = doc.createElement("linescolor");
			t = doc.createTextNode(fmt.linescolor.name());
			f.appendChild(t);
			c.appendChild(f);
		}
		
		f = doc.createElement("fontFamily");
		t = doc.createTextNode(fmt.fontFamily);
		f.appendChild(t);
		c.appendChild(f);

		f = doc.createElement("pointSize");
		t = doc.createTextNode(QString("%1").arg(fmt.pointSize));
		f.appendChild(t);
		c.appendChild(f);

        f = doc.createElement("wrapAround");
        t = doc.createTextNode(fmt.wrapAround ? "true" : "false");
        f.appendChild(t);
        c.appendChild(f);

		elem.appendChild(c);
	}
}

/*!
	\overload
	\brief Load format data from a QSettings object
	\param s QSettings object from which data will be fetched
	\param ignoreNewIds whether unknown format identifiers should be ignored
	
	The QSettings object is assumed to be initialized properly and to
	point to a correct location.
	
	\note Previous content is not discarded
*/
void QFormatScheme::load(QSettings& s, bool ignoreNewIds)
{
	if (s.childKeys().isEmpty() && s.childGroups().isEmpty()) return;

	QString version = s.value("version").toString();

	if ( version < QFORMAT_VERSION )
	{
		qWarning("Format encoding version mismatch : [found]%s != [expected]%s",
				qPrintable(version),
				QFORMAT_VERSION);
		
		return;
	}
	
	s.beginGroup("data");
	
	QStringList l = s.childGroups();
	
	foreach ( QString id, l )
	{
		if ( ignoreNewIds && !m_formatKeys.contains(id) )
			continue;
		
		s.beginGroup(id);
		
		QFormat fmt;
		QStringList fields = s.childKeys();
		
		foreach ( QString field, fields )
		{
			QString value = s.value(field).toString();
			setFormatOption(fmt, field, value);
			
		}
		fmt.setPriority(fmt.priority); //update priority if other values changed

		setFormat(id, fmt);
		s.endGroup();
	}
	
	s.endGroup();
}

/*!
	\overload
*/
void QFormatScheme::save(QSettings& s,QFormatScheme *defaultFormats) const
{
	s.setValue("version", QFORMAT_VERSION);
	
	s.beginGroup("data");
	
	for ( int i = 0; i < m_formatKeys.count(); ++i )
	{
		const QFormat& fmt = m_formatValues.at(i);
		if(defaultFormats && fmt==defaultFormats->m_formatValues.at(i)){
		    s.remove(m_formatKeys.at(i));
		    continue;
		}

		s.beginGroup(m_formatKeys.at(i));
		
		s.setValue("priority", fmt.priority);
		s.setValue("bold", (fmt.weight == QFont::Bold) ? "true" : "false");
		s.setValue("italic", fmt.italic ? "true" : "false");
		s.setValue("overline", fmt.overline ? "true" : "false");
		s.setValue("underline", fmt.underline ? "true" : "false");
		s.setValue("strikeout", fmt.strikeout ? "true" : "false");
		s.setValue("waveUnderline", fmt.waveUnderline ? "true" : "false");
		
		if ( fmt.foreground.isValid() )
		{
			s.setValue("foreground", fmt.foreground.name());
		} else {
			s.remove("foreground");
		}
		
		if ( fmt.background.isValid() )
		{
			s.setValue("background", fmt.background.name());
		} else {
			s.remove("background");
		}
		
		if ( fmt.linescolor.isValid() )
		{
			s.setValue("linescolor", fmt.linescolor.name());
		} else {
			s.remove("linescolor");
		}
		
		s.setValue("fontFamily", fmt.fontFamily);
		s.setValue("pointSize", fmt.pointSize);
        s.setValue("wrapAround", fmt.wrapAround ? "true" : "false");

		s.endGroup();
	}
	
	s.endGroup();
}

/*!
	\return The number of available formats
*/
int QFormatScheme::formatCount() const
{
	return m_formatValues.count();
}

/*!
	\return A list of available format keys
*/
QStringList QFormatScheme::formats() const
{
	return m_formatKeys.toList();
}

/*!
	\return The format key associated to integer format id \a ifid
*/
QString QFormatScheme::id(int ifid) const
{
	return m_formatKeys.at(ifid);
}

/*!
	\return The integer format id associated to format key \a sfid
*/
int QFormatScheme::id(const QString& sfid) const
{
	int idx = m_formatKeys.indexOf(sfid);
	
	return (idx == -1) ? 0 : idx;
}

/*!
 * \return true if the format key \a sfid is valid
 */
bool QFormatScheme::exists(const QString& sfid) const
{
	return m_formatKeys.contains(sfid);
}

/*!
	\return The text format associated with format key \a fid
	
	\warning Use at your own risks : if there are no format associated
	with the requested id this function will crash
*/
QFormat& QFormatScheme::formatRef(int ifid)
{
	return m_formatValues[ifid];
}

/*!
	\return The a reference to the text format associated with format key \a fid
	
	\warning Use at your own risks : if there are no format associated
	with the requested id this function will crash.
*/
QFormat& QFormatScheme::formatRef(const QString& sfid)
{
	return m_formatValues[id(sfid)];
}

/*!
	\return The text format associated with format key \a fid
*/
QFormat QFormatScheme::format(int ifid) const
{
	//qDebug("Querying format id %i within ]-1, %i[", ifid, m_formatValues.count());
	
	return (ifid < m_formatValues.count()) ? m_formatValues.at(ifid) : QFormat();
}

/*!
	\return The text format associated with format key \a fid
*/
QFormat QFormatScheme::format(const QString& sfid) const
{
	return format(id(sfid));
}

/*!
	\brief Set text format for key
	\param fid Format key
	\param fmt Format value
*/
void QFormatScheme::setFormat(const QString& fid, const QFormat& fmt)
{
	const int idx = m_formatKeys.indexOf(fid);
	
	if ( idx != -1 )
	{
		modified |= (m_formatValues[idx] != fmt);
		m_formatValues[idx] = fmt;
	} else {
		
		//qDebug("adding format %s [%i]", qPrintable(fid), m_formatKeys.count());
		
		m_formatKeys << fid;
		m_formatValues << fmt;
	}
}

void QFormatScheme::mergeFormats(int &oldFormat, int newFormat){
	oldFormat = ((oldFormat << FORMAT_SHIFT) & FORMAT_MASK_FULL) | newFormat;
	/*	if ((m_cache[i] & FORMAT_MASK_BASE) == r.format) continue;
		else if (((m_cache[i]<<FORMAT_SHIFT) & FORMAT_MASK_BASE) == r.format) continue;
		else if (((m_cache[i]<<(2*FORMAT_SHIFT)) & FORMAT_MASK_BASE) == r.format) continue;
		else m_cache[i] = ((m_cache[i] << FORMAT_SHIFT) & FORMAT_MASK_FULL) | r.format;*/
		//qDebug("%i: %x ", i, m_cache [i]);
}
void QFormatScheme::extractFormats(int mergedFormat, int* fmt, QFormat* formats, int &fontFormat) const{
//this doesn't require a valid "this"
	fmt[0] = mergedFormat & FORMAT_MASK_BASE;
	fmt[1] = (mergedFormat >> (FORMAT_SHIFT)) & FORMAT_MASK_BASE;
	fmt[2] = (mergedFormat >> (2*FORMAT_SHIFT)) & FORMAT_MASK_BASE;

	if (!this) return;

	formats[0] = format(fmt[0]);
	formats[1] = format(fmt[1]);
	formats[2] = format(fmt[2]);

	if ( formats[0].realPriority < formats[1].realPriority ) {
		if ( formats[1].realPriority < formats[2].realPriority ) fontFormat = fmt[2];
		else fontFormat = fmt[1];
	} else {
		if ( formats[0].realPriority < formats[2].realPriority ) fontFormat = fmt[2];
		else fontFormat = fmt[0];
	}
}


QString QFormatScheme::exportAsCSS(bool simplifyCSS){
	Q_ASSERT( m_formatKeys.size() == m_formatValues.size() );
	QString result;
	for (int i = 0; i < m_formatValues.size(); i++ )
		result += QString(".fmt%1 { %2 } /* %3 */\n").arg(i).arg(m_formatValues[i].toCSS(simplifyCSS)).arg(m_formatKeys[i]);
	return result;
}

/*! @} */