File: xmltagslistwidget.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 (150 lines) | stat: -rw-r--r-- 4,699 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
/***************************************************************************
 *   copyright       : (C) 2003-2009 by Pascal Brachet                     *
 *   http://www.xm1math.net/texmaker/                                      *
 *   inspired by the ktikz (GPL) program from Glad Deschrijver             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "xmltagslistwidget.h"

#include <QListWidget>
#include <QFont>
#include <QColor>
#include <QDomDocument>
#include <QFile>
#include <QApplication>
#include <QDebug>

XmlTagsListWidget::XmlTagsListWidget(QWidget *parent, QString file): QListWidget(parent)
{
	mFile = file;
	mLoaded = false;
}

void XmlTagsListWidget::populate()
{
	if (!mLoaded) {
		QFile tagsFile(mFile);
		if (tagsFile.open(QFile::ReadOnly)) {
			QDomDocument domDocument;
			if (domDocument.setContent(&tagsFile)) {
				QDomElement root = domDocument.documentElement();
				if (root.tagName() == "texmakertags") {
					xmlSections = getTags(root);
					for (int i = 0; i < xmlSections.children.size(); ++i)
						addListWidgetItems(xmlSections.children.at(i));
				}
			}
		}
		mLoaded = true;
	}
}

void XmlTagsListWidget::showEvent(QShowEvent *)
{
	if (!mLoaded)
		populate();
}

bool XmlTagsListWidget::isPopulated()
{
	return mLoaded;
}

QStringList XmlTagsListWidget::tagsTxtFromCategory(const QString &category)
{
	foreach  (const xmlTagList &tags, xmlSections.children)
		if (tags.id == category) {
			QStringList result;
			foreach (const xmlTag &tag, tags.tags)
				result.append(tag.txt);
			return result;
		}
	return QStringList();
}

QString tagsFromTagTxtRec(const xmlTagList &tagList, const QString &tagTxt)
{
	foreach (const xmlTag &tag, tagList.tags)
		if (tag.txt == tagTxt) return tag.tag;
	QString result;
	foreach (const xmlTagList &childTagList, tagList.children) {
		result = tagsFromTagTxtRec(childTagList, tagTxt);
		if (!result.isEmpty())
			return result;
	}
	return result;
}

QString XmlTagsListWidget::tagsFromTagTxt(const QString &tagTxt)
{
	return tagsFromTagTxtRec(xmlSections, tagTxt);
}

xmlTagList XmlTagsListWidget::getTags(const QDomElement &element)
{
	xmlTag item;
	xmlTagList tagList;
	QList<xmlTag> tags;
	tagList.title = element.attribute("title");
	tagList.id = element.attribute("id");
	QDomElement child = element.firstChildElement("item");
	QString txt, code, type;
	while (!child.isNull()) {
		code = child.attribute("tag");
		code.replace("\\\\", "\\");
		code.replace("&lt;", "<");
		code.replace("&gt;", ">");
		item.tag = code;
		txt = child.attribute("txt");
		if (txt != "") {
			txt.replace("\\\\", "\\");
			txt.replace("&lt;", "<");
			txt.replace("&gt;", ">");
		} else txt = code;
		item.txt = txt.remove(QRegExp("%[<n>|]?"));
		item.type = child.attribute("type").toInt();
		tags << item;
		child = child.nextSiblingElement("item");
	}
	tagList.tags = tags;
	QDomElement section = element.firstChildElement("section");
	while (!section.isNull()) {
		tagList.children << getTags(section);
		section = section.nextSiblingElement("section");
	}
	return tagList;
}

void XmlTagsListWidget::addListWidgetItems(const xmlTagList &tagList)
{
	QFont titleFont = qApp->font();
	titleFont.setBold(true);
	QFont optionFont = qApp->font();
	optionFont.setItalic(true);
	QFont commandFont = qApp->font();
	//QColor titleBg(QApplication::style()->standardPalette().color(QPalette::Normal, QPalette::Highlight));
	//QColor titleFg(QApplication::style()->standardPalette().color(QPalette::Normal, QPalette::HighlightedText));
	QColor titleBg("#447BCD");
	QColor titleFg("#ffffff");
	QListWidgetItem *item = new QListWidgetItem(this);
	QString itemText = tagList.title;
	item->setText(itemText);
	item->setBackgroundColor(titleBg);
	item->setTextColor(titleFg);
	item->setFont(titleFont);
	for (int i = 0; i < tagList.tags.size(); ++i) {
		QListWidgetItem *item = new QListWidgetItem(this);
		QString itemText = tagList.tags.at(i).txt;
		item->setText(itemText);
		item->setData(Qt::UserRole, tagList.tags.at(i).tag);
		if (tagList.tags.at(i).type == 0) item->setFont(commandFont);
		else item->setFont(optionFont);
	}
	//TODO: use QListView, and CompletionItemDelegate from latexcompleter to highlight temporary code completion
}