• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

lokalize

  • sources
  • kde-4.14
  • kdesdk
  • lokalize
  • src
  • tm
qamodel.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Lokalize
3  Copyright (C) 2011-2012 Nick Shaforostoff <shafff@ukr.net>
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 
20 #include "qamodel.h"
21 #include "domroutines.h"
22 #include <QStringList>
23 #include <klocalizedstring.h>
24 #include <QFile>
25 #include <QTextStream>
26 #include <QCoreApplication>
27 
28 static QString ruleTagNames[]={QString("source"), QString("falseFriend"), QString("target")};
29 
30 static QStringList domListToStringList(const QDomNodeList& nodes)
31 {
32  QStringList result;
33  result.reserve(nodes.size());
34  for (int i=0;i<nodes.size();i++)
35  result.append(nodes.at(i).toElement().text());
36 
37  return result;
38 }
39 
40 static QRegExp domNodeToRegExp(const QDomNode& node)
41 {
42  QRegExp re(node.toElement().text());
43  re.setMinimal(true);
44  return re;
45 }
46 
47 static QVector<QRegExp> domListToRegExpVector(const QDomNodeList& nodes)
48 {
49  QVector<QRegExp> result;
50  result.reserve(nodes.size());
51  for (int i=0;i<nodes.size();i++)
52  result.append(domNodeToRegExp(nodes.at(i)));
53 
54  return result;
55 }
56 
57 
58 QaModel* QaModel::_instance=0;
59 void QaModel::cleanupQaModel()
60 {
61  delete QaModel::_instance; QaModel::_instance = 0;
62 }
63 
64 bool QaModel::isInstantiated()
65 {
66  return _instance!=0;
67 }
68 
69 QaModel* QaModel::instance()
70 {
71  if (KDE_ISUNLIKELY( _instance==0 )) {
72  _instance=new QaModel;
73  qAddPostRoutine(QaModel::cleanupQaModel);
74  }
75 
76  return _instance;
77 }
78 
79 
80 QaModel::QaModel(QObject* parent): QAbstractListModel(parent)
81 {
82 }
83 
84 QaModel::~QaModel()
85 {
86  saveRules();
87 }
88 
89 int QaModel::rowCount(const QModelIndex& parent) const
90 {
91  if (parent.isValid())
92  return 0;
93  return m_entries.count();
94 }
95 
96 QVariant QaModel::headerData(int section, Qt::Orientation , int role) const
97 {
98  if (role!=Qt::DisplayRole)
99  return QVariant();
100 
101  switch (section)
102  {
103  //case ID: return i18nc("@title:column","ID");
104  case Source: return i18nc("@title:column Original text","Source");;
105  case FalseFriend: return i18nc("@title:column Translator's false friend","False Friend");
106  }
107  return QVariant();
108 }
109 
110 
111 QVariant QaModel::data(const QModelIndex& item, int role) const
112 {
113  if (role==Qt::ToolTipRole)
114  return m_filename;
115 
116  if (role!=Qt::DisplayRole && role!=Qt::EditRole)
117  return QVariant();
118 
119  static const QString nl("\n");
120  const QDomElement& entry=m_entries.at(item.row()).toElement();
121  return domListToStringList(entry.elementsByTagName(ruleTagNames[item.column()])).join(nl);
122  return QVariant();
123 }
124 
125 QVector<Rule> QaModel::toVector() const
126 {
127  QVector<Rule> rules;
128  QDomNodeList m_categories=m_doc.elementsByTagName("category");
129  for (int i=0;i<m_categories.size();i++)
130  {
131  static const QString ruleTagName("rule");
132  QDomNodeList m_rules=m_categories.at(i).toElement().elementsByTagName(ruleTagName);
133  for (int j=0;j<m_rules.size();j++)
134  {
135  Rule rule;
136  rule.sources=domListToRegExpVector(m_rules.at(j).toElement().elementsByTagName(ruleTagNames[Source]));
137  rule.falseFriends=domListToRegExpVector(m_rules.at(j).toElement().elementsByTagName(ruleTagNames[FalseFriend]));
138  rule.targets=domListToRegExpVector(m_rules.at(j).toElement().elementsByTagName("target"));
139  rules.append(rule);
140  }
141  }
142  return rules;
143 }
144 
145 bool QaModel::loadRules(const QString& filename)
146 {
147  QFile file(filename);
148  if (file.open(QIODevice::ReadOnly))
149  {
150  bool ok=m_doc.setContent(&file);
151  file.close();
152  if (!ok)
153  return false;
154  }
155  else
156  {
157  m_doc.setContent(QByteArray(
158 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
159 "<qa version=\"1.0\">\n"
160 " <category name=\"default\">\n"
161 " </category>\n"
162 "</qa>\n"));
163  }
164 
165  m_entries=m_doc.elementsByTagName("rule");
166  m_filename=filename;
167  return true;
168 }
169 
170 bool QaModel::saveRules(QString filename)
171 {
172  if (filename.isEmpty())
173  filename=m_filename;
174 
175  if (filename.isEmpty())
176  return false;
177 
178  QFile device(filename);
179  if (!device.open(QFile::WriteOnly | QFile::Truncate))
180  return false;
181  QTextStream stream(&device);
182  m_doc.save(stream,2);
183 
184  //setClean(true);
185  return true;
186 }
187 
188 
189 QModelIndex QaModel::appendRow()
190 {
191  beginInsertRows(QModelIndex(),rowCount(),rowCount());
192 
193  QDomElement category=m_doc.elementsByTagName("qa").at(0).toElement().elementsByTagName("category").at(0).toElement();
194  QDomElement rule=category.appendChild(m_doc.createElement("rule")).toElement();
195  rule.appendChild(m_doc.createElement(ruleTagNames[Source]));
196  rule.appendChild(m_doc.createElement(ruleTagNames[FalseFriend]));
197 
198  endInsertRows();
199 
200  return index(m_entries.count()-1);
201 }
202 
203 void QaModel::removeRow(const QModelIndex& rowIndex)
204 {
205  //TODO optimize for contiguous selections
206  beginRemoveRows(QModelIndex(),rowIndex.row(),rowIndex.row());
207 
208  QDomElement category=m_doc.elementsByTagName("qa").at(0).toElement().elementsByTagName("category").at(0).toElement();
209  category.removeChild(m_entries.at(rowIndex.row()));
210 
211  endRemoveRows();
212 }
213 
214 
215 Qt::ItemFlags QaModel::flags(const QModelIndex& ) const
216 {
217  return Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsEditable;
218 }
219 
220 bool QaModel::setData(const QModelIndex& item, const QVariant& value, int role)
221 {
222  if (role!=Qt::DisplayRole && role!=Qt::EditRole)
223  return false;
224 
225  QDomElement entry=m_entries.at(item.row()).toElement();
226  QDomNodeList sources=entry.elementsByTagName(ruleTagNames[item.column()]);
227 
228  QStringList newSources=value.toString().split('\n');
229  while(sources.size()<newSources.size())
230  entry.insertAfter(m_doc.createElement(ruleTagNames[item.column()]), sources.at(sources.size()-1));
231 
232  while(sources.size()>newSources.size())
233  entry.removeChild(sources.at(sources.size()-1));
234 
235  for (int i=0;i<sources.size();i++)
236  setText(sources.at(i).toElement(), newSources.at(i));
237 
238  emit dataChanged(item, item);
239  return true;
240 }
241 
QModelIndex
QDomElement::elementsByTagName
QDomNodeList elementsByTagName(const QString &tagname) const
QaModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Definition: qamodel.cpp:220
QDomNode::appendChild
QDomNode appendChild(const QDomNode &newChild)
QVector::append
void append(const T &value)
QByteArray
QRegExp::setMinimal
void setMinimal(bool minimal)
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
QList::reserve
void reserve(int alloc)
QDomNode::insertAfter
QDomNode insertAfter(const QDomNode &newChild, const QDomNode &refChild)
QDomNodeList
QList::at
const T & at(int i) const
Rule::falseFriends
QVector< QRegExp > falseFriends
Definition: rule.h:43
Rule::targets
QVector< QRegExp > targets
Definition: rule.h:42
QDomNode
QStringList::join
QString join(const QString &separator) const
QFile
QaModel::~QaModel
~QaModel()
Definition: qamodel.cpp:84
QTextStream
QList::size
int size() const
QaModel::QaModel
QaModel(QObject *parent=0)
Definition: qamodel.cpp:80
ruleTagNames
static QString ruleTagNames[]
Definition: qamodel.cpp:28
QDomNode::toElement
QDomElement toElement() const
QRegExp
QModelIndex::isValid
bool isValid() const
QDomNodeList::count
int count() const
QList::append
void append(const T &value)
QaModel::appendRow
QModelIndex appendRow()
Definition: qamodel.cpp:189
QaModel::saveRules
bool saveRules(QString filename=QString())
Definition: qamodel.cpp:170
QDomElement::text
QString text() const
setText
void setText(QDomElement element, QString text)
Definition: domroutines.cpp:26
QAbstractItemModel::dataChanged
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
QAbstractItemModel::endInsertRows
void endInsertRows()
QObject
QAbstractListModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
QAbstractListModel
QaModel::removeRow
void removeRow(const QModelIndex &)
Definition: qamodel.cpp:203
QDomDocument::elementsByTagName
QDomNodeList elementsByTagName(const QString &tagname) const
QaModel::Source
Definition: qamodel.h:40
QString::isEmpty
bool isEmpty() const
QAbstractItemModel::beginRemoveRows
void beginRemoveRows(const QModelIndex &parent, int first, int last)
QModelIndex::row
int row() const
Rule
Definition: rule.h:39
domroutines.h
QaModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: qamodel.cpp:89
QString
Rule::sources
QVector< QRegExp > sources
Definition: rule.h:41
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QaModel::flags
Qt::ItemFlags flags(const QModelIndex &) const
Definition: qamodel.cpp:215
QStringList
QaModel
Definition: qamodel.h:32
QVector::reserve
void reserve(int size)
QDomNode::removeChild
QDomNode removeChild(const QDomNode &oldChild)
QFile::close
virtual void close()
QAbstractItemModel::beginInsertRows
void beginInsertRows(const QModelIndex &parent, int first, int last)
domListToRegExpVector
static QVector< QRegExp > domListToRegExpVector(const QDomNodeList &nodes)
Definition: qamodel.cpp:47
QaModel::data
QVariant data(const QModelIndex &, int role=Qt::DisplayRole) const
Definition: qamodel.cpp:111
domListToStringList
static QStringList domListToStringList(const QDomNodeList &nodes)
Definition: qamodel.cpp:30
QDomNode::save
void save(QTextStream &str, int indent) const
domNodeToRegExp
static QRegExp domNodeToRegExp(const QDomNode &node)
Definition: qamodel.cpp:40
QVector< QRegExp >
QaModel::loadRules
bool loadRules(const QString &filename)
Definition: qamodel.cpp:145
QModelIndex::column
int column() const
QaModel::headerData
QVariant headerData(int section, Qt::Orientation, int role=Qt::DisplayRole) const
Definition: qamodel.cpp:96
QaModel::isInstantiated
static bool isInstantiated()
Definition: qamodel.cpp:64
QDomNodeList::size
int size() const
QDomDocument::createElement
QDomElement createElement(const QString &tagName)
QAbstractItemModel::endRemoveRows
void endRemoveRows()
QDomElement
QVariant::toString
QString toString() const
QaModel::instance
static QaModel * instance()
Definition: qamodel.cpp:69
QaModel::FalseFriend
Definition: qamodel.h:41
qamodel.h
QaModel::toVector
QVector< Rule > toVector() const
Definition: qamodel.cpp:125
QDomDocument::setContent
bool setContent(const QByteArray &data, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
QDomNodeList::at
QDomNode at(int index) const
QVariant
Qt::ItemFlags
typedef ItemFlags
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:40:07 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

lokalize

Skip menu "lokalize"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal