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

« back to all changes in this revision

Viewing changes to lokalize/src/tm/tmwindow.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 "tmwindow.h"
 
34
#include "ui_queryoptions.h"
 
35
#include "project.h"
 
36
#include "dbfilesmodel.h"
 
37
 
 
38
#include <klocale.h>
 
39
#include <kstandarddirs.h>
 
40
 
 
41
 
 
42
#include <QTreeView>
 
43
#include <QSqlQueryModel>
 
44
#include <QButtonGroup>
 
45
 
 
46
//BEGIN TMDBModel
 
47
TMDBModel::TMDBModel(QObject* parent)
 
48
    : QSqlQueryModel(parent)
 
49
    , m_queryType(WordOrder)
 
50
{
 
51
    setHeaderData(0, Qt::Horizontal, i18nc("@title:column Original text","Original"));
 
52
    setHeaderData(1, Qt::Horizontal, i18nc("@title:column Text in target language","Target"));
 
53
}
 
54
 
 
55
void TMDBModel::setDB(const QString& str)
 
56
{
 
57
    m_db=QSqlDatabase::database(str);
 
58
}
 
59
 
 
60
void TMDBModel::setQueryType(int type)
 
61
{
 
62
    m_queryType=(QueryType)type;
 
63
}
 
64
 
 
65
void TMDBModel::setFilter(const QString& str)
 
66
{
 
67
    QString escaped(str);
 
68
    escaped.replace('\'',"''");
 
69
 
 
70
    if (m_queryType==SubStr)
 
71
    {
 
72
        setQuery("SELECT tm_main.english, tm_main.target FROM tm_main "
 
73
                 "WHERE tm_main.english LIKE '%"+escaped+"%' "
 
74
                 "OR tm_main.target LIKE '%"+escaped+"%' "
 
75
                 "UNION "
 
76
                 "SELECT tm_main.english, tm_dups.target FROM tm_main, tm_dups "
 
77
                 "WHERE tm_main.id==tm_dups.id "
 
78
                 "AND (tm_main.english LIKE '%"+escaped+"%' "
 
79
                 "OR tm_dups.target LIKE '%"+escaped+"%') "
 
80
                    /*"ORDER BY tm_main.english"*/ ,m_db);
 
81
    }
 
82
    else if (m_queryType==WordOrder)
 
83
    {
 
84
        QStringList strList=str.split(QRegExp("\\W"),QString::SkipEmptyParts);
 
85
        setQuery("SELECT tm_main.english, tm_main.target FROM tm_main "
 
86
                 "WHERE tm_main.english LIKE '%"+
 
87
                 strList.join("%' AND tm_main.english LIKE '%")+
 
88
                 "%' "
 
89
                 "UNION "
 
90
                 "SELECT tm_main.english, tm_main.target FROM tm_main "
 
91
                 "WHERE tm_main.target LIKE '%"+
 
92
                 strList.join("%' AND tm_main.target LIKE '%")+
 
93
                 "%' "
 
94
                 "UNION "
 
95
                 "SELECT tm_main.english, tm_dups.target FROM tm_main, tm_dups "
 
96
                 "WHERE tm_main.id==tm_dups.id "
 
97
                 "AND tm_main.english LIKE '%"+
 
98
                 strList.join("%' AND tm_main.english LIKE '%")+
 
99
                 "%' "
 
100
                 "UNION "
 
101
                 "SELECT tm_main.english, tm_dups.target FROM tm_main, tm_dups "
 
102
                 "WHERE tm_main.id==tm_dups.id "
 
103
                 "AND tm_dups.target LIKE '%"+
 
104
                 strList.join("%' AND tm_dups.target LIKE '%")+
 
105
                 "%' "
 
106
                    /*"ORDER BY tm_main.english"*/ ,m_db);
 
107
    }
 
108
    else //regex
 
109
    {
 
110
 
 
111
        setQuery("SELECT tm_main.english, tm_main.target FROM tm_main "
 
112
                 "WHERE tm_main.english GLOB '*"+escaped+"*' "
 
113
                 "OR tm_main.target GLOB '*"+escaped+"*' "
 
114
                 "UNION "
 
115
                 "SELECT tm_main.english, tm_dups.target FROM tm_main, tm_dups "
 
116
                 "WHERE tm_main.id==tm_dups.id "
 
117
                 "AND (tm_main.english GLOB '*"+escaped+"*' "
 
118
                 "OR tm_dups.target GLOB '*"+escaped+"*') "
 
119
                    /*"ORDER BY tm_main.english"*/ ,m_db);
 
120
 
 
121
    }
 
122
 
 
123
 
 
124
}
 
125
 
 
126
//END TMDBModel
 
127
 
 
128
 
 
129
//BEGIN TMWindow
 
130
 
 
131
TMWindow::TMWindow(QWidget *parent)
 
132
 : KMainWindow(parent)
 
133
{
 
134
    setCaption(i18nc("@title:window","Translation Memory Query"),false);
 
135
 
 
136
    QWidget* w=new QWidget(this);
 
137
    Ui_QueryOptions ui_queryOptions;
 
138
    ui_queryOptions.setupUi(w);
 
139
    setCentralWidget(w);
 
140
 
 
141
    connect(ui_queryOptions.query,SIGNAL(returnPressed()),
 
142
           this,SLOT(performQuery()));
 
143
 
 
144
    QTreeView* view=ui_queryOptions.treeView;
 
145
    m_query=ui_queryOptions.query;
 
146
 
 
147
    m_model = new TMDBModel(this);
 
148
    m_model->setDB(Project::instance()->projectID());
 
149
 
 
150
    view->setModel(m_model);
 
151
 
 
152
    QButtonGroup* btnGrp=new QButtonGroup(this);
 
153
    btnGrp->addButton(ui_queryOptions.substr,(int)TMDBModel::SubStr);
 
154
    btnGrp->addButton(ui_queryOptions.like,(int)TMDBModel::WordOrder);
 
155
    btnGrp->addButton(ui_queryOptions.rx,(int)TMDBModel::RegExp);
 
156
    connect(btnGrp,SIGNAL(buttonClicked(int)),
 
157
            m_model,SLOT(setQueryType(int)));
 
158
 
 
159
    ui_queryOptions.db->setModel(DBFilesModel::instance());
 
160
    ui_queryOptions.db->setCurrentIndex(ui_queryOptions.db->findText(Project::instance()->projectID()));
 
161
    connect(ui_queryOptions.db,SIGNAL(currentIndexChanged(QString)),
 
162
            m_model,SLOT(setDB(QString)));
 
163
 
 
164
    m_dbCombo=ui_queryOptions.db;
 
165
 
 
166
    m_dbCombo->setCurrentIndex(m_dbCombo->findText(Project::instance()->projectID()));
 
167
}
 
168
 
 
169
TMWindow::~TMWindow()
 
170
{
 
171
}
 
172
 
 
173
void TMWindow::selectDB(int i)
 
174
{
 
175
    m_dbCombo->setCurrentIndex(i);
 
176
}
 
177
 
 
178
void TMWindow::performQuery()
 
179
{
 
180
    m_model->setFilter(m_query->text());
 
181
}
 
182
/*
 
183
void TMWindow::setOptions(int i)
 
184
{
 
185
    
 
186
}*/
 
187
 
 
188
 
 
189
 
 
190
//END TMWindow