~ubuntu-branches/ubuntu/intrepid/kdesdk/intrepid-updates

« back to all changes in this revision

Viewing changes to lokalize/src/glossary/glossaryview.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2008-05-28 10:11:43 UTC
  • mto: This revision was merged to the branch mainline in revision 37.
  • Revision ID: james.westby@ubuntu.com-20080528101143-gzc3styjz1b70zxu
Tags: upstream-4.0.80
ImportĀ upstreamĀ versionĀ 4.0.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ****************************************************************************
 
2
  This file is part of KAider
 
3
 
 
4
  Copyright (C) 2007 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
 
 
41
 
 
42
#include <klineedit.h>
 
43
#include <kdialog.h>
 
44
#include <klocale.h>
 
45
#include <kdebug.h>
 
46
#include <kurl.h>
 
47
 
 
48
#include <QDragEnterEvent>
 
49
#include <QTime>
 
50
#include <QSet>
 
51
#include <QAction>
 
52
// #include <QShortcutEvent>
 
53
 
 
54
 
 
55
 
 
56
GlossaryView::GlossaryView(QWidget* parent,Catalog* catalog,const QVector<QAction*>& actions)
 
57
        : QDockWidget ( i18nc("@title:window","Glossary"), parent)
 
58
        , m_browser(new QWidget(this))
 
59
        , m_catalog(catalog)
 
60
        , m_flowLayout(new FlowLayout(FlowLayout::glossary,m_browser,this,actions,0,10))
 
61
        , m_glossary(Project::instance()->glossary())
 
62
        , m_rxClean(Project::instance()->markup()+'|'+Project::instance()->accel())//cleaning regexp; NOTE isEmpty()?
 
63
        , m_rxSplit("\\W|\\d")//splitting regexp
 
64
        , m_currentIndex(-1)
 
65
        , m_normTitle(i18nc("@title:window","Glossary"))
 
66
        , m_hasInfoTitle(m_normTitle+" [*]")
 
67
        , m_hasInfo(false)
 
68
 
 
69
{
 
70
    setObjectName("glossaryView");
 
71
    setWidget(m_browser);
 
72
    m_browser->setLayout(m_flowLayout);
 
73
    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>)."));
 
74
 
 
75
    m_browser->setAutoFillBackground(true);
 
76
    m_browser->setBackgroundRole(QPalette::Background);
 
77
 
 
78
    m_rxClean.setMinimal(true);
 
79
    connect (m_glossary,SIGNAL(changed()),this,SLOT(slotNewEntryDisplayed()));
 
80
}
 
81
 
 
82
GlossaryView::~GlossaryView()
 
83
{
 
84
}
 
85
 
 
86
 
 
87
//TODO define new term by dragging some text.
 
88
// void GlossaryView::dragEnterEvent(QDragEnterEvent* event)
 
89
// {
 
90
//     /*    if(event->mimeData()->hasUrls() && event->mimeData()->urls().first().path().endsWith(".po"))
 
91
//         {
 
92
//             event->acceptProposedAction();
 
93
//         };*/
 
94
// }
 
95
// 
 
96
// void GlossaryView::dropEvent(QDropEvent *event)
 
97
// {
 
98
//         event->acceptProposedAction();*/
 
99
// }
 
100
 
 
101
void GlossaryView::slotNewEntryDisplayed(uint entry)
 
102
{
 
103
    QTime time;time.start();
 
104
    if (entry==0xffffffff)
 
105
        entry=m_currentIndex;
 
106
    else
 
107
        m_currentIndex=entry;
 
108
 
 
109
    if (m_catalog->numberOfEntries()<=(int)entry)
 
110
        return;//because of Qt::QueuedConnection
 
111
    //kWarning()<<"m_catalog->numberOfEntries()"<<m_catalog->numberOfEntries()<<entry;
 
112
//     if (!toggleViewAction()->isChecked())
 
113
//         return;
 
114
    Glossary& glossary=*m_glossary;
 
115
 
 
116
 
 
117
 
 
118
    QString msg(m_catalog->msgid(entry).toLower());
 
119
    msg.remove(m_rxClean);
 
120
 
 
121
//     QRegExp accel(Project::instance()->accel());
 
122
//     kWarning()<<endl<<endl<<"valvalvalvalval " <<Project::instance()->accel()<<endl;
 
123
//     int pos=0;
 
124
//     while ((pos=accel.indexIn(msg,pos))!=-1)
 
125
//     {
 
126
//         msg.remove(accel.pos(1),accel.cap(1).size());
 
127
//         pos=accel.pos(1);
 
128
//     }
 
129
 
 
130
    QStringList words(msg.split(m_rxSplit,QString::SkipEmptyParts));
 
131
    if (words.isEmpty())
 
132
    {
 
133
        if (m_hasInfo)
 
134
        {
 
135
            m_flowLayout->clearTerms();
 
136
            m_hasInfo=false;
 
137
            setWindowTitle(m_normTitle);
 
138
        }
 
139
        return;
 
140
    }
 
141
 
 
142
    kDebug()<<"1";
 
143
    QList<int> termIndexes;
 
144
    int i=0;
 
145
    for (;i<words.size();++i)
 
146
    {
 
147
        if (glossary.wordHash.contains(words.at(i))
 
148
//             && MULTI hash!! instead, we generate QSet later
 
149
//             !termIndexes.contains(glossary.wordHash.value(words.at(i)))
 
150
           )
 
151
        {
 
152
//             kWarning()<<"val " <<glossary.wordHash.values(words.at(i));
 
153
            termIndexes+=glossary.wordHash.values(words.at(i));
 
154
        }
 
155
    }
 
156
    if (termIndexes.isEmpty())
 
157
    {
 
158
        if (m_hasInfo)
 
159
        {
 
160
            m_flowLayout->clearTerms();
 
161
            m_hasInfo=false;
 
162
            setWindowTitle(m_normTitle);
 
163
        }
 
164
        return;
 
165
    }
 
166
    // we found entries that contain words from msgid
 
167
    setUpdatesEnabled(false);
 
168
 
 
169
    if (m_hasInfo)
 
170
        m_flowLayout->clearTerms();
 
171
 
 
172
    bool found=false;
 
173
    m_flowLayout->setEnabled(false);
 
174
    int j; //INTJ haha! socionics!
 
175
    QSet<int> termIndexesSet(termIndexes.toSet());
 
176
//     kWarning()<<"found";
 
177
    QSet<int>::const_iterator it = termIndexesSet.constBegin();
 
178
    kDebug()<<"2";
 
179
    while (it != termIndexesSet.constEnd())
 
180
    {
 
181
        // now check which of them are really hits...
 
182
        int lim=glossary.termList.at(*it).english.size();
 
183
        for (j=0;j<lim;++j)
 
184
        {
 
185
            // ...and if so, which part of termEn list we must thank for match ...
 
186
            if (msg.contains(
 
187
                glossary.termList.at(*it).english.at(j)//,
 
188
                //Qt::CaseInsensitive  //we lowered terms on load 
 
189
                        )
 
190
                )
 
191
            {
 
192
                //insert it into label
 
193
                found=true;
 
194
                m_flowLayout->addTerm(
 
195
                        glossary.termList.at(*it).english.at(j),
 
196
                        *it
 
197
                               );
 
198
                break;
 
199
            }
 
200
        }
 
201
        ++it;
 
202
    }
 
203
    m_flowLayout->setEnabled(true);
 
204
 
 
205
    if (found)
 
206
    {
 
207
        if (!m_hasInfo)
 
208
        {
 
209
            m_hasInfo=true;
 
210
            setWindowTitle(m_hasInfoTitle);
 
211
        }
 
212
    }
 
213
    else if (m_hasInfo)
 
214
    {
 
215
        m_hasInfo=true;
 
216
        setWindowTitle(m_hasInfoTitle);
 
217
    }
 
218
 
 
219
    setUpdatesEnabled(true);
 
220
    kWarning()<<"ELA "<<time.elapsed();
 
221
}
 
222
 
 
223
 
 
224
 
 
225
#include "glossaryview.moc"