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

lokalize

  • sources
  • kde-4.12
  • kdesdk
  • lokalize
  • src
  • glossary
glossaryview.cpp
Go to the documentation of this file.
1 /* ****************************************************************************
2  This file is part of Lokalize
3 
4  Copyright (C) 2007-2011 by Nick Shaforostoff <shafff@ukr.net>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20  In addition, as a special exception, the copyright holders give
21  permission to link the code of this program with any edition of
22  the Qt library by Trolltech AS, Norway (or with modified versions
23  of Qt that use the same license as Qt), and distribute linked
24  combinations including the two. You must obey the GNU General
25  Public License in all respects for all of the code used other than
26  Qt. If you modify this file, you may extend this exception to
27  your version of the file, but you are not obligated to do so. If
28  you do not wish to do so, delete this exception statement from
29  your version.
30 
31 **************************************************************************** */
32 
33 #include "glossaryview.h"
34 #include "glossary.h"
35 #include "project.h"
36 #include "catalog.h"
37 #include "flowlayout.h"
38 
39 #include "glossarywindow.h"
40 #include "stemming.h"
41 
42 #include <klineedit.h>
43 #include <kdialog.h>
44 #include <klocale.h>
45 #include <kdebug.h>
46 #include <kurl.h>
47 #include <kaction.h>
48 
49 #include <QDragEnterEvent>
50 #include <QTime>
51 #include <QSet>
52 #include <QScrollArea>
53 // #include <QShortcutEvent>
54 #include <QPushButton>
55 
56 
57 
58 using namespace GlossaryNS;
59 
60 GlossaryView::GlossaryView(QWidget* parent,Catalog* catalog,const QVector<KAction*>& actions)
61  : QDockWidget ( i18nc("@title:window","Glossary"), parent)
62  , m_browser(new QScrollArea(this))
63  , m_catalog(catalog)
64  , m_flowLayout(new FlowLayout(FlowLayout::glossary,/*who gets signals*/this,actions,0,10))
65  , m_glossary(Project::instance()->glossary())
66  , m_rxClean(Project::instance()->markup()+'|'+Project::instance()->accel())//cleaning regexp; NOTE isEmpty()?
67  , m_rxSplit("\\W|\\d")//splitting regexp
68  , m_currentIndex(-1)
69  , m_normTitle(i18nc("@title:window","Glossary"))
70  , m_hasInfoTitle(m_normTitle+" [*]")
71  , m_hasInfo(false)
72 
73 {
74  setObjectName("glossaryView");
75  QWidget* w=new QWidget(m_browser);
76  m_browser->setWidget(w);
77  m_browser->setWidgetResizable(true);
78  w->setLayout(m_flowLayout);
79  w->show();
80 
81  setToolTip(i18nc("@info:tooltip","<p>Translations for common terms appear here.</p>"
82  "<p>Press shortcut displayed near the term to insert its translation.</p>"
83  "<p>Use context menu to add new entry (tip:&nbsp;select words in original and translation fields before calling <interface>Define&nbsp;new&nbsp;term</interface>).</p>"));
84 
85  setWidget(m_browser);
86  m_browser->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
87  m_browser->setAutoFillBackground(true);
88  m_browser->setBackgroundRole(QPalette::Background);
89 
90  m_rxClean.setMinimal(true);
91  connect (m_glossary,SIGNAL(changed()),this,SLOT(slotNewEntryDisplayed()),Qt::QueuedConnection);
92 }
93 
94 GlossaryView::~GlossaryView()
95 {
96 }
97 
98 
99 //TODO define new term by dragging some text.
100 // void GlossaryView::dragEnterEvent(QDragEnterEvent* event)
101 // {
102 // /* if(event->mimeData()->hasUrls() && event->mimeData()->urls().first().path().endsWith(".po"))
103 // {
104 // event->acceptProposedAction();
105 // };*/
106 // }
107 //
108 // void GlossaryView::dropEvent(QDropEvent *event)
109 // {
110 // event->acceptProposedAction();*/
111 // }
112 
113 void GlossaryView::slotNewEntryDisplayed(DocPosition pos)
114 {
115  //kWarning()<<"\n\n\n\nstart"<<pos.entry<<m_currentIndex;
116  QTime time;time.start();
117  if (pos.entry==-1)
118  pos.entry=m_currentIndex;
119  else
120  m_currentIndex=pos.entry;
121 
122  if (pos.entry==-1 || m_catalog->numberOfEntries()<=pos.entry)
123  return;//because of Qt::QueuedConnection
124  //if (!toggleViewAction()->isChecked())
125  // return;
126 
127  Glossary& glossary=*m_glossary;
128 
129 
130  QString source=m_catalog->source(pos);
131  QString sourceLowered=source.toLower();
132  QString msg=sourceLowered;
133  msg.remove(m_rxClean);
134  QString msgStemmed;
135 
136 // QRegExp accel(Project::instance()->accel());
137 // kWarning()<<endl<<endl<<"valvalvalvalval " <<Project::instance()->accel()<<endl;
138 // int pos=0;
139 // while ((pos=accel.indexIn(msg,pos))!=-1)
140 // {
141 // msg.remove(accel.pos(1),accel.cap(1).size());
142 // pos=accel.pos(1);
143 // }
144 
145  QString sourceLangCode=Project::instance()->sourceLangCode();
146  QList<QByteArray> termIds;
147  foreach (const QString& w, msg.split(m_rxSplit,QString::SkipEmptyParts))
148  {
149  QString word=stem(sourceLangCode,w);
150  QList<QByteArray> indexes=glossary.idsForLangWord(sourceLangCode,word);
151  //if (indexes.size())
152  //kWarning()<<"found entry for:" <<word;
153  termIds+=indexes;
154  msgStemmed+=word+' ';
155  }
156  if (termIds.isEmpty())
157  return clear();
158 
159  // we found entries that contain words from msgid
160  setUpdatesEnabled(false);
161 
162  if (m_hasInfo)
163  m_flowLayout->clearTerms();
164 
165  bool found=false;
166  //m_flowLayout->setEnabled(false);
167  foreach (const QByteArray& termId, termIds.toSet())
168  {
169  // now check which of them are really hits...
170  foreach (const QString& enTerm, glossary.terms(termId, sourceLangCode))
171  {
172  // ...and if so, which part of termEn list we must thank for match ...
173  bool ok=msg.contains(enTerm);//,//Qt::CaseInsensitive //we lowered terms on load
174  if (!ok)
175  {
176  QString enTermStemmed;
177  foreach (const QString& word, enTerm.split(m_rxSplit,QString::SkipEmptyParts))
178  enTermStemmed+=stem(sourceLangCode,word)+' ';
179  ok=msgStemmed.contains(enTermStemmed);
180  }
181  if (ok)
182  {
183  //insert it into label
184  found=true;
185  int pos=sourceLowered.indexOf(enTerm);
186  m_flowLayout->addTerm(enTerm,termId,/*uppercase*/pos!=-1 && source.at(pos).isUpper());
187  break;
188  }
189  }
190  }
191  //m_flowLayout->setEnabled(true);
192 
193  if (!found)
194  clear();
195  else if (!m_hasInfo)
196  {
197  m_hasInfo=true;
198  setWindowTitle(m_hasInfoTitle);
199  }
200 
201  setUpdatesEnabled(true);
202 }
203 
204 void GlossaryView::clear()
205 {
206  if (m_hasInfo)
207  {
208  m_flowLayout->clearTerms();
209  m_hasInfo=false;
210  setWindowTitle(m_normTitle);
211  }
212 }
213 
214 #include "glossaryview.moc"
GlossaryNS::GlossaryView::GlossaryView
GlossaryView(QWidget *, Catalog *, const QVector< KAction * > &)
Definition: glossaryview.cpp:60
project.h
stem
QString stem(const QString &langCode, const QString &word)
Definition: stemming.cpp:72
QDockWidget
Project::instance
static Project * instance()
Definition: project.cpp:67
QWidget
FlowLayout::addTerm
void addTerm(const QString &term, const QByteArray &entryId, bool capFirst=false)
Definition: flowlayout.cpp:180
DocPosition::entry
int entry
Definition: pos.h:48
DocPosition
This struct represents a position in a catalog.
Definition: pos.h:38
Project
Singleton object that represents project.
Definition: project.h:51
GlossaryNS::Glossary
Internal representation of glossary.
Definition: glossary.h:95
catalog.h
glossarywindow.h
FlowLayout::clearTerms
void clearTerms()
Definition: flowlayout.cpp:171
glossary.h
GlossaryNS::GlossaryView::~GlossaryView
~GlossaryView()
Definition: glossaryview.cpp:94
GlossaryNS::Glossary::idsForLangWord
QList< QByteArray > idsForLangWord(const QString &lang, const QString &word) const
Definition: glossary.cpp:53
stemming.h
GlossaryNS::GlossaryView::slotNewEntryDisplayed
void slotNewEntryDisplayed(DocPosition pos=DocPosition())
Definition: glossaryview.cpp:113
GlossaryNS::Glossary::terms
QStringList terms(const QByteArray &id, const QString &lang) const
Definition: glossary.cpp:337
glossaryview.h
Catalog::numberOfEntries
int numberOfEntries() const
Definition: catalog.cpp:171
Catalog::source
QString source(const DocPosition &pos) const
Definition: catalog.h:95
Catalog
This class represents a catalog It uses CatalogStorage interface to work with catalogs in different f...
Definition: catalog.h:74
flowlayout.h
FlowLayout
used in glossary and kross views
Definition: flowlayout.h:47
Project::sourceLangCode
Q_SCRIPTABLE QString sourceLangCode() const
Definition: project.h:88
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:03:45 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
  • okteta
  • 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