lokalize
glossaryview.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "glossaryview.h"
00034 #include "glossary.h"
00035 #include "project.h"
00036 #include "catalog.h"
00037 #include "flowlayout.h"
00038
00039 #include "glossarywindow.h"
00040
00041
00042 #include <klineedit.h>
00043 #include <kdialog.h>
00044 #include <klocale.h>
00045 #include <kdebug.h>
00046 #include <kurl.h>
00047 #include <kaction.h>
00048
00049 #include <QDragEnterEvent>
00050 #include <QTime>
00051 #include <QSet>
00052 #include <QScrollArea>
00053
00054
00055
00056 using namespace GlossaryNS;
00057
00058 GlossaryView::GlossaryView(QWidget* parent,Catalog* catalog,const QVector<KAction*>& actions)
00059 : QDockWidget ( i18nc("@title:window","Glossary"), parent)
00060 , m_browser(new QScrollArea(this))
00061 , m_catalog(catalog)
00062 , m_flowLayout(new FlowLayout(FlowLayout::glossary,m_browser,this,actions,0,10))
00063 , m_glossary(Project::instance()->glossary())
00064 , m_rxClean(Project::instance()->markup()+'|'+Project::instance()->accel())
00065 , m_rxSplit("\\W|\\d")
00066 , m_currentIndex(-1)
00067 , m_normTitle(i18nc("@title:window","Glossary"))
00068 , m_hasInfoTitle(m_normTitle+" [*]")
00069 , m_hasInfo(false)
00070
00071 {
00072 setObjectName("glossaryView");
00073 setWidget(m_browser);
00074 QWidget* w=new QWidget(m_browser);
00075 m_browser->setWidget(w);
00076 w->setLayout(m_flowLayout);
00077 setToolTip(i18nc("@info:tooltip","Translations to common terms appear here. Press displayed shortcut to insert term translation. Use context menu to add new entry (tip: select words in original and translation fields before calling <interface>Define new term</interface>)."));
00078
00079 m_browser->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
00080 m_browser->setAutoFillBackground(true);
00081 m_browser->setBackgroundRole(QPalette::Background);
00082
00083 m_rxClean.setMinimal(true);
00084 connect (m_glossary,SIGNAL(changed()),this,SLOT(slotNewEntryDisplayed()));
00085 }
00086
00087 GlossaryView::~GlossaryView()
00088 {
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 void GlossaryView::slotNewEntryDisplayed(DocPosition pos)
00107 {
00108
00109 QTime time;time.start();
00110 if (pos.entry==-1)
00111 pos.entry=m_currentIndex;
00112 else
00113 m_currentIndex=pos.entry;
00114
00115 if (pos.entry==-1 || m_catalog->numberOfEntries()<=pos.entry)
00116 return;
00117
00118
00119
00120
00121 Glossary& glossary=*m_glossary;
00122
00123
00124
00125 QString msg(m_catalog->source(pos).toLower());
00126 msg.remove(m_rxClean);
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 QStringList words(msg.split(m_rxSplit,QString::SkipEmptyParts));
00138 if (words.isEmpty())
00139 {
00140 if (m_hasInfo)
00141 {
00142 m_flowLayout->clearTerms();
00143 m_hasInfo=false;
00144 setWindowTitle(m_normTitle);
00145 }
00146 return;
00147 }
00148
00149 QList<int> termIndexes;
00150 int i=0;
00151 for (;i<words.size();++i)
00152 {
00153 if (glossary.wordHash.contains(words.at(i))
00154
00155
00156 )
00157 {
00158
00159 termIndexes+=glossary.wordHash.values(words.at(i));
00160 }
00161 }
00162 if (termIndexes.isEmpty())
00163 {
00164 if (m_hasInfo)
00165 {
00166 m_flowLayout->clearTerms();
00167 m_hasInfo=false;
00168 setWindowTitle(m_normTitle);
00169 }
00170 return;
00171 }
00172
00173 setUpdatesEnabled(false);
00174
00175 if (m_hasInfo)
00176 m_flowLayout->clearTerms();
00177
00178 bool found=false;
00179 m_flowLayout->setEnabled(false);
00180 int j;
00181 QSet<int> termIndexesSet(termIndexes.toSet());
00182
00183 QSet<int>::const_iterator it = termIndexesSet.constBegin();
00184 kDebug()<<"2";
00185 while (it != termIndexesSet.constEnd())
00186 {
00187
00188 int lim=glossary.termList.at(*it).english.size();
00189 for (j=0;j<lim;++j)
00190 {
00191
00192 if (msg.contains(
00193 glossary.termList.at(*it).english.at(j)
00194
00195 )
00196 )
00197 {
00198
00199 found=true;
00200 m_flowLayout->addTerm(
00201 glossary.termList.at(*it).english.at(j),
00202 *it
00203 );
00204 break;
00205 }
00206 }
00207 ++it;
00208 }
00209 m_flowLayout->setEnabled(true);
00210
00211 if (found)
00212 {
00213 if (!m_hasInfo)
00214 {
00215 m_hasInfo=true;
00216 setWindowTitle(m_hasInfoTitle);
00217 }
00218 }
00219 else if (m_hasInfo)
00220 {
00221 m_hasInfo=true;
00222 setWindowTitle(m_hasInfoTitle);
00223 }
00224
00225 setUpdatesEnabled(true);
00226 kWarning()<<"ELA "<<time.elapsed();
00227 }
00228
00229
00230
00231 #include "glossaryview.moc"