• 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
alttransview.cpp
Go to the documentation of this file.
1 /* ****************************************************************************
2  This file is part of Lokalize
3 
4  Copyright (C) 2007-2008 by Nick Shaforostoff <shafff@ukr.net>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License as
8  published by the Free Software Foundation; either version 2 of
9  the License or (at your option) version 3 or any later version
10  accepted by the membership of KDE e.V. (or its successor approved
11  by the membership of KDE e.V.), which shall act as a proxy
12  defined in Section 14 of version 3 of the license.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program. If not, see <http://www.gnu.org/licenses/>.
21 
22 **************************************************************************** */
23 
24 #define KDE_NO_DEBUG_OUTPUT
25 
26 #include "alttransview.h"
27 #include <QDragEnterEvent>
28 
29 #include "diff.h"
30 #include "catalog.h"
31 #include "cmd.h"
32 #include "project.h"
33 #include "xlifftextedit.h"
34 #include "tmview.h" //TextBrowser
35 #include "mergecatalog.h"
36 #include "prefs_lokalize.h"
37 
38 #include <klocale.h>
39 #include <kdebug.h>
40 #include <kaction.h>
41 
42 #include <QSignalMapper>
43 //#include <QTime>
44 #include <QFileInfo>
45 #include <QToolTip>
46 
47 
48 AltTransView::AltTransView(QWidget* parent, Catalog* catalog,const QVector<KAction*>& actions)
49  : QDockWidget ( i18nc("@title:window","Alternate Translations"), parent)
50  , m_browser(new TM::TextBrowser(this))
51  , m_catalog(catalog)
52  , m_normTitle(i18nc("@title:window","Alternate Translations"))
53  , m_hasInfoTitle(m_normTitle+" [*]")
54  , m_hasInfo(false)
55  , m_actions(actions)
56 {
57  setObjectName("msgIdDiff");
58  setWidget(m_browser);
59  hide();
60 
61  m_browser->setReadOnly(true);
62  m_browser->viewport()->setBackgroundRole(QPalette::Background);
63  QTimer::singleShot(0,this,SLOT(initLater()));
64 }
65 
66 void AltTransView::initLater()
67 {
68  setAcceptDrops(true);
69 
70  KConfig config;
71  KConfigGroup group(&config,"AltTransView");
72  m_everShown=group.readEntry("EverShown",false);
73 
74 
75 
76  QSignalMapper* signalMapper=new QSignalMapper(this);
77  int i=m_actions.size();
78  while(--i>=0)
79  {
80  connect(m_actions.at(i),SIGNAL(triggered()),signalMapper,SLOT(map()));
81  signalMapper->setMapping(m_actions.at(i), i);
82  }
83  connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(slotUseSuggestion(int)));
84 
85  connect(m_browser,SIGNAL(textInsertRequested(QString)),this,SIGNAL(textInsertRequested(QString)));
86  //connect(m_browser,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(contextMenu(QPoint)));
87 }
88 
89 AltTransView::~AltTransView()
90 {
91 }
92 
93 void AltTransView::dragEnterEvent(QDragEnterEvent* event)
94 {
95  if(event->mimeData()->hasUrls() && Catalog::extIsSupported(event->mimeData()->urls().first().path()))
96  event->acceptProposedAction();
97 }
98 
99 void AltTransView::dropEvent(QDropEvent *event)
100 {
101  event->acceptProposedAction();
102  attachAltTransFile(event->mimeData()->urls().first().toLocalFile());
103 
104  //update
105  m_prevEntry.entry=-1;
106  QTimer::singleShot(0,this,SLOT(process()));
107 }
108 
109 void AltTransView::attachAltTransFile(const QString& path)
110 {
111  MergeCatalog* altCat=new MergeCatalog(m_catalog, m_catalog, /*saveChanges*/false);
112  altCat->loadFromUrl(KUrl::fromLocalFile(path));
113  m_catalog->attachAltTransCatalog(altCat);
114 }
115 
116 void AltTransView::addAlternateTranslation(int entry, const QString& trans, bool temp)
117 {
118  qDebug()<<trans;
119  AltTrans altTrans;
120  altTrans.target=trans;
121  m_catalog->attachAltTrans(entry, altTrans);
122 
123  m_prevEntry=DocPos();
124  QTimer::singleShot(0,this,SLOT(process()));
125 }
126 
127 void AltTransView::fileLoaded()
128 {
129  m_prevEntry.entry=-1;
130  QString absPath=m_catalog->url().toLocalFile();
131  QString relPath=KUrl::relativePath(Project::instance()->projectDir(),absPath);
132 
133  QFileInfo info(Project::instance()->altTransDir()+'/'+relPath);
134  if (info.canonicalFilePath()!=absPath && info.exists())
135  attachAltTransFile(info.canonicalFilePath());
136  else
137  qWarning()<<"alt trans file doesn't exist:"<<info.canonicalFilePath();
138 }
139 
140 void AltTransView::slotNewEntryDisplayed(const DocPosition& pos)
141 {
142  m_entry=DocPos(pos);
143  QTimer::singleShot(0,this,SLOT(process()));
144 }
145 
146 void AltTransView::process()
147 {
148  if (m_entry==m_prevEntry) return;
149  if (m_catalog->numberOfEntries()<=m_entry.entry)
150  return;//because of Qt::QueuedConnection
151 
152  m_prevEntry=m_entry;
153  m_browser->clear();
154  m_entryPositions.clear();
155 
156  const QVector<AltTrans>& entries=m_catalog->altTrans(m_entry.toDocPosition());
157  m_entries=entries;
158 
159  if (entries.isEmpty())
160  {
161  if (m_hasInfo)
162  {
163  m_hasInfo=false;
164  setWindowTitle(m_normTitle);
165  }
166  return;
167  }
168  if (!m_hasInfo)
169  {
170  m_hasInfo=true;
171  setWindowTitle(m_hasInfoTitle);
172  }
173 
174 
175  CatalogString source=m_catalog->sourceWithTags(m_entry.toDocPosition());
176 
177  QTextBlockFormat blockFormatBase;
178  QTextBlockFormat blockFormatAlternate; blockFormatAlternate.setBackground(QPalette().alternateBase());
179  QTextCharFormat noncloseMatchCharFormat;
180  QTextCharFormat closeMatchCharFormat; closeMatchCharFormat.setFontWeight(QFont::Bold);
181  int i=0;
182  int limit=entries.size();
183  forever
184  {
185  const AltTrans& entry=entries.at(i);
186 
187  QTextCursor cur=m_browser->textCursor();
188  QString html;
189  html.reserve(1024);
190  if (!entry.source.isEmpty())
191  {
192  html+="<p>";
193 
194  QString result=Qt::escape(userVisibleWordDiff(entry.source.string, source.string,Project::instance()->accel(),Project::instance()->markup()));
195  //result.replace("&","&amp;");
196  //result.replace("<","&lt;");
197  //result.replace(">","&gt;");
198  result.replace("{KBABELADD}","<font style=\"background-color:"+Settings::addColor().name()+";color:black\">");
199  result.replace("{/KBABELADD}","</font>");
200  result.replace("{KBABELDEL}","<font style=\"background-color:"+Settings::delColor().name()+";color:black\">");
201  result.replace("{/KBABELDEL}","</font>");
202  result.replace("\\n","\\n<br>");
203  result.replace("\\n","\\n<br>");
204 
205  html+=result;
206  html+="<br>";
207  cur.insertHtml(html); html.clear();
208  }
209  if (!entry.target.isEmpty())
210  {
211  if (KDE_ISLIKELY( i<m_actions.size() ))
212  {
213  m_actions.at(i)->setStatusTip(entry.target.string);
214  html+=QString("[%1] ").arg(m_actions.at(i)->shortcut().toString());
215  }
216  else
217  html+="[ - ] ";
218 
219  cur.insertText(html); html.clear();
220  insertContent(cur,entry.target);
221  }
222  m_entryPositions.insert(cur.anchor(),i);
223 
224  html+=i?"<br></p>":"</p>";
225  cur.insertHtml(html);
226 
227  if (KDE_ISUNLIKELY( ++i>=limit ))
228  break;
229 
230  cur.insertBlock(i%2?blockFormatAlternate:blockFormatBase);
231  }
232 
233 
234  if (!m_everShown)
235  {
236  m_everShown=true;
237  show();
238 
239  KConfig config;
240  KConfigGroup group(&config,"AltTransView");
241  group.writeEntry("EverShown",true);
242  }
243 }
244 
245 
246 bool AltTransView::event(QEvent *event)
247 {
248  if (event->type()==QEvent::ToolTip)
249  {
250  QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event);
251 
252  if (m_entryPositions.isEmpty())
253  {
254  QString tooltip=i18nc("@info:tooltip","<p>Sometimes, if source text is changed, its translation becomes deprecated and is either marked as <emphasis>needing&nbsp;review</emphasis> (i.e. looses approval status), "
255  "or (only in case of XLIFF file) moved to the <emphasis>alternate&nbsp;translations</emphasis> section accompanying the unit.</p>"
256  "<p>This toolview also shows the difference between current source string and the previous source string, so that you can easily see which changes should be applied to existing translation to make it reflect current source.</p>"
257  "<p>Double-clicking any word in this toolview inserts it into translation.</p>"
258  "<p>Drop translation file onto this toolview to use it as a source for additional alternate translations.</p>"
259  );
260  QToolTip::showText(helpEvent->globalPos(),tooltip);
261  return true;
262  }
263 
264  int block1=m_browser->cursorForPosition(m_browser->viewport()->mapFromGlobal(helpEvent->globalPos())).blockNumber();
265  int block=*m_entryPositions.lowerBound(m_browser->cursorForPosition(m_browser->viewport()->mapFromGlobal(helpEvent->globalPos())).anchor());
266  if (block1!=block)
267  kWarning()<<"block numbers don't match";
268  if (block>=m_entries.size())
269  return false;
270 
271  QString origin=m_entries.at(block).origin;
272  if (origin.isEmpty())
273  return false;
274 
275  QString tooltip=i18nc("@info:tooltip","Origin: %1",origin);
276  QToolTip::showText(helpEvent->globalPos(),tooltip);
277  return true;
278  }
279  return QWidget::event(event);
280 }
281 
282 void AltTransView::slotUseSuggestion(int i)
283 {
284  if (KDE_ISUNLIKELY( i>=m_entries.size() ))
285  return;
286 
287  TM::TMEntry tmEntry;
288  tmEntry.target=m_entries.at(i).target;
289  CatalogString source=m_catalog->sourceWithTags(m_entry.toDocPosition());
290  tmEntry.diff=userVisibleWordDiff(m_entries.at(i).source.string, source.string,Project::instance()->accel(),Project::instance()->markup());
291 
292  CatalogString target=TM::targetAdapted(tmEntry, source);
293 
294  kWarning()<<"0"<<target.string;
295  if (KDE_ISUNLIKELY( target.isEmpty() ))
296  return;
297 
298  m_catalog->beginMacro(i18nc("@item Undo action","Use alternate translation"));
299 
300  QString old=m_catalog->targetWithTags(m_entry.toDocPosition()).string;
301  if (!old.isEmpty())
302  {
303  //FIXME test!
304  removeTargetSubstring(m_catalog, m_entry.toDocPosition(), 0, old.size());
305  //m_catalog->push(new DelTextCmd(m_catalog,m_pos,m_catalog->msgstr(m_pos)));
306  }
307  kWarning()<<"1"<<target.string;
308 
309  //m_catalog->push(new InsTextCmd(m_catalog,m_pos,target)/*,true*/);
310  insertCatalogString(m_catalog, m_entry.toDocPosition(), target, 0);
311 
312  m_catalog->endMacro();
313 
314  emit refreshRequested();
315 }
316 
317 
318 #include "alttransview.moc"
AltTransView::addAlternateTranslation
void addAlternateTranslation(int entry, const QString &, bool temp=true)
Definition: alttransview.cpp:116
QEvent
MergeCatalog
Merge source container.
Definition: mergecatalog.h:71
project.h
QWidget
Catalog::url
const KUrl & url() const
Definition: catalog.h:189
Catalog::targetWithTags
CatalogString targetWithTags(const DocPosition &pos) const
Definition: catalog.cpp:211
Catalog::altTrans
QVector< AltTrans > altTrans(const DocPosition &pos) const
Definition: catalog.cpp:272
QEvent::type
Type type() const
QTextCursor
QDropEvent::mimeData
const QMimeData * mimeData() const
QTextCursor::insertHtml
void insertHtml(const QString &html)
QDockWidget
QUndoStack::beginMacro
void beginMacro(const QString &text)
QTextBlockFormat
userVisibleWordDiff
QString userVisibleWordDiff(const QString &str1ForMatching, const QString &str2ForMatching, const QString &accel, const QString &markup, int options)
Definition: diff.cpp:408
Project::instance
static Project * instance()
Definition: project.cpp:67
QString::size
int size() const
AltTransView::textInsertRequested
void textInsertRequested(const QString &)
ProjectBase::markup
QString markup() const
Get Markup.
Definition: projectbase.h:252
CatalogString::string
QString string
Definition: catalogstring.h:130
cmd.h
AltTransView::AltTransView
AltTransView(QWidget *, Catalog *, const QVector< KAction * > &)
Definition: alttransview.cpp:48
insertContent
void insertContent(QTextCursor &cursor, const CatalogString &catStr, const CatalogString &refStr, bool insertText)
Definition: xlifftextedit.cpp:344
Settings::delColor
static QColor delColor()
Get DelColor.
Definition: prefs_lokalize.h:133
QMap::clear
void clear()
Catalog::attachAltTrans
void attachAltTrans(int entry, const AltTrans &trans)
Definition: catalog.cpp:267
QToolTip::showText
void showText(const QPoint &pos, const QString &text, QWidget *w)
QTextCursor::anchor
int anchor() const
TM::targetAdapted
CatalogString targetAdapted(const TMEntry &entry, const CatalogString &ref)
this tries some black magic naturally, there are many assumptions that might not always be true ...
Definition: tmview.cpp:660
QString::clear
void clear()
Catalog::extIsSupported
static bool extIsSupported(const QString &path)
Definition: catalog.cpp:93
DocPosition
This struct represents a position in a catalog.
Definition: pos.h:38
TM::TMEntry
Definition: tmentry.h:35
CatalogString::isEmpty
bool isEmpty() const
Definition: catalogstring.h:144
QObject::name
const char * name() const
ProjectBase::accel
QString accel() const
Get Accel.
Definition: projectbase.h:235
QSignalMapper::setMapping
void setMapping(QObject *sender, int id)
QFileInfo::canonicalFilePath
QString canonicalFilePath() const
QTextCursor::insertBlock
void insertBlock()
removeTargetSubstring
bool removeTargetSubstring(Catalog *catalog, DocPosition pos, int delStart, int delLen)
Definition: cmd.cpp:364
AltTransView::~AltTransView
~AltTransView()
Definition: alttransview.cpp:89
AltTransView::fileLoaded
void fileLoaded()
Definition: alttransview.cpp:127
Settings::addColor
static QColor addColor()
Get AddColor.
Definition: prefs_lokalize.h:115
QHelpEvent::globalPos
const QPoint & globalPos() const
catalog.h
QDropEvent
QTextCursor::insertText
void insertText(const QString &text)
QObject::setObjectName
void setObjectName(const QString &name)
QString::isEmpty
bool isEmpty() const
QTextFormat::setBackground
void setBackground(const QBrush &brush)
QString
QWidget::hide
void hide()
mergecatalog.h
diff.h
AltTransView::slotNewEntryDisplayed
void slotNewEntryDisplayed(const DocPosition &)
Definition: alttransview.cpp:140
QMap::lowerBound
iterator lowerBound(const Key &key)
QWidget::setAcceptDrops
void setAcceptDrops(bool on)
QFileInfo
QTextCharFormat
QFileInfo::exists
bool exists() const
TM::TMEntry::target
CatalogString target
Definition: tmentry.h:38
AltTrans::target
CatalogString target
Definition: alttrans.h:37
TM::TMEntry::diff
QString diff
Definition: tmentry.h:53
QDockWidget::setWidget
void setWidget(QWidget *widget)
QTextCharFormat::setFontWeight
void setFontWeight(int weight)
QString::replace
QString & replace(int position, int n, QChar after)
CatalogString
data structure used to pass info about inline elements a XLIFF tag is represented by a TAGRANGE_IMAGE...
Definition: catalogstring.h:128
xlifftextedit.h
QVector::at
const T & at(int i) const
insertCatalogString
void insertCatalogString(Catalog *catalog, DocPosition pos, const CatalogString &catStr, int start)
Definition: cmd.cpp:421
alttransview.h
QMimeData::hasUrls
bool hasUrls() const
QVector< KAction * >
QDragEnterEvent
prefs_lokalize.h
QMimeData::urls
QList< QUrl > urls() const
QUndoStack::endMacro
void endMacro()
Qt::escape
QString escape(const QString &plain)
QVector::isEmpty
bool isEmpty() const
Catalog::attachAltTransCatalog
void attachAltTransCatalog(Catalog *)
Definition: catalog.cpp:260
Catalog::sourceWithTags
CatalogString sourceWithTags(const DocPosition &pos) const
Definition: catalog.cpp:203
QWidget::setWindowTitle
void setWindowTitle(const QString &)
Catalog::numberOfEntries
int numberOfEntries() const
Definition: catalog.cpp:171
AltTrans::source
CatalogString source
Definition: alttrans.h:36
DocPos
simpler version of DocPosition for use in QMap
Definition: pos.h:82
QString::reserve
void reserve(int size)
DocPos::entry
int entry
Definition: pos.h:84
AltTransView::refreshRequested
void refreshRequested()
Catalog
This class represents a catalog It uses CatalogStorage interface to work with catalogs in different f...
Definition: catalog.h:74
QMap::insert
iterator insert(const Key &key, const T &value)
QWidget::show
void show()
QMap::isEmpty
bool isEmpty() const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QVector::size
int size() const
QHelpEvent
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QWidget::event
virtual bool event(QEvent *event)
QSignalMapper
DocPos::toDocPosition
DocPosition toDocPosition()
Definition: pos.h:107
AltTrans
Definition: alttrans.h:30
QPalette
QtConcurrent::mapped
QFuture< T > mapped(const Sequence &sequence, MapFunction function)
tmview.h
QTimer::singleShot
singleShot
MergeCatalog::loadFromUrl
int loadFromUrl(const KUrl &url)
Definition: mergecatalog.cpp:163
AltTransView::attachAltTransFile
void attachAltTransFile(const QString &)
Definition: alttransview.cpp:109
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:40:06 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