1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Linguist of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "batchtranslationdialog.h"
43 #include "phrase.h"
44 #include "messagemodel.h"
45 
46 #include <QtGui/QMessageBox>
47 #include <QtGui/QProgressDialog>
48 
49 QT_BEGIN_NAMESPACE
50 
CheckableListModel(QObject * parent)51 CheckableListModel::CheckableListModel(QObject *parent)
52   : QStandardItemModel(parent)
53 {
54 }
55 
flags(const QModelIndex & index) const56 Qt::ItemFlags CheckableListModel::flags(const QModelIndex &index) const
57 {
58     Q_UNUSED(index);
59     return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
60 }
61 
BatchTranslationDialog(MultiDataModel * dataModel,QWidget * w)62 BatchTranslationDialog::BatchTranslationDialog(MultiDataModel *dataModel, QWidget *w)
63  : QDialog(w), m_model(this), m_dataModel(dataModel)
64 {
65     m_ui.setupUi(this);
66     connect(m_ui.runButton, SIGNAL(clicked()), this, SLOT(startTranslation()));
67     connect(m_ui.moveUpButton, SIGNAL(clicked()), this, SLOT(movePhraseBookUp()));
68     connect(m_ui.moveDownButton, SIGNAL(clicked()), this, SLOT(movePhraseBookDown()));
69 
70     m_ui.phrasebookList->setModel(&m_model);
71     m_ui.phrasebookList->setSelectionBehavior(QAbstractItemView::SelectItems);
72     m_ui.phrasebookList->setSelectionMode(QAbstractItemView::SingleSelection);
73 }
74 
75 
setPhraseBooks(const QList<PhraseBook * > & phrasebooks,int modelIndex)76 void BatchTranslationDialog::setPhraseBooks(const QList<PhraseBook *> &phrasebooks, int modelIndex)
77 {
78     QString fn = QFileInfo(m_dataModel->srcFileName(modelIndex)).baseName();
79     setWindowTitle(tr("Batch Translation of '%1' - Qt Linguist").arg(fn));
80     m_model.clear();
81     m_model.insertColumn(0);
82     m_phrasebooks = phrasebooks;
83     m_modelIndex = modelIndex;
84     int count = phrasebooks.count();
85     m_model.insertRows(0, count);
86     for (int i = 0; i < count; ++i) {
87         QModelIndex idx(m_model.index(i, 0));
88         m_model.setData(idx, phrasebooks[i]->friendlyPhraseBookName());
89         int sortOrder;
90         if (phrasebooks[i]->language() != QLocale::C
91             && m_dataModel->language(m_modelIndex) != QLocale::C) {
92             if (phrasebooks[i]->language() != m_dataModel->language(m_modelIndex))
93                 sortOrder = 3;
94             else
95                 sortOrder = (phrasebooks[i]->country()
96                              == m_dataModel->model(m_modelIndex)->country()) ? 0 : 1;
97         } else {
98             sortOrder = 2;
99         }
100         m_model.setData(idx, sortOrder == 3 ? Qt::Unchecked : Qt::Checked, Qt::CheckStateRole);
101         m_model.setData(idx, sortOrder, Qt::UserRole + 1);
102         m_model.setData(idx, i, Qt::UserRole);
103     }
104     m_model.setSortRole(Qt::UserRole + 1);
105     m_model.sort(0);
106 }
107 
startTranslation()108 void BatchTranslationDialog::startTranslation()
109 {
110     int translatedcount = 0;
111     QCursor oldCursor = cursor();
112     setCursor(Qt::BusyCursor);
113     int messageCount = m_dataModel->messageCount();
114 
115     QProgressDialog *dlgProgress;
116     dlgProgress = new QProgressDialog(tr("Searching, please wait..."), tr("&Cancel"), 0, messageCount, this);
117     dlgProgress->show();
118 
119     int msgidx = 0;
120     const bool translateTranslated = m_ui.ckTranslateTranslated->isChecked();
121     const bool translateFinished = m_ui.ckTranslateFinished->isChecked();
122     for (MultiDataModelIterator it(m_dataModel, m_modelIndex); it.isValid(); ++it) {
123         if (MessageItem *m = it.current()) {
124             if (!m->isObsolete()
125                 && (translateTranslated || m->translation().isEmpty())
126                 && (translateFinished || !m->isFinished())) {
127 
128                 // Go through them in the order the user specified in the phrasebookList
129                 for (int b = 0; b < m_model.rowCount(); ++b) {
130                     QModelIndex idx(m_model.index(b, 0));
131                     QVariant checkState = m_model.data(idx, Qt::CheckStateRole);
132                     if (checkState == Qt::Checked) {
133                         PhraseBook *pb = m_phrasebooks[m_model.data(idx, Qt::UserRole).toInt()];
134                         foreach (const Phrase *ph, pb->phrases()) {
135                             if (ph->source() == m->text()) {
136                                 m_dataModel->setTranslation(it, ph->target());
137                                 m_dataModel->setFinished(it, m_ui.ckMarkFinished->isChecked());
138                                 ++translatedcount;
139                                 goto done; // break 2;
140                             }
141                         }
142                     }
143                 }
144             }
145         }
146       done:
147         ++msgidx;
148         if (!(msgidx & 15))
149             dlgProgress->setValue(msgidx);
150         qApp->processEvents();
151         if (dlgProgress->wasCanceled())
152             break;
153     }
154     dlgProgress->hide();
155 
156     setCursor(oldCursor);
157     emit finished();
158     QMessageBox::information(this, tr("Linguist batch translator"),
159         tr("Batch translated %n entries", "", translatedcount), QMessageBox::Ok);
160 }
161 
movePhraseBookUp()162 void BatchTranslationDialog::movePhraseBookUp()
163 {
164     QModelIndexList indexes = m_ui.phrasebookList->selectionModel()->selectedIndexes();
165     if (indexes.count() <= 0) return;
166 
167     QModelIndex sel = indexes[0];
168     int row = sel.row();
169     if (row > 0) {
170         QModelIndex other = m_model.index(row - 1, 0);
171         QMap<int, QVariant> seldata = m_model.itemData(sel);
172         m_model.setItemData(sel, m_model.itemData(other));
173         m_model.setItemData(other, seldata);
174         m_ui.phrasebookList->selectionModel()->setCurrentIndex(other, QItemSelectionModel::ClearAndSelect);
175     }
176 }
177 
movePhraseBookDown()178 void BatchTranslationDialog::movePhraseBookDown()
179 {
180     QModelIndexList indexes = m_ui.phrasebookList->selectionModel()->selectedIndexes();
181     if (indexes.count() <= 0) return;
182 
183     QModelIndex sel = indexes[0];
184     int row = sel.row();
185     if (row < m_model.rowCount() - 1) {
186         QModelIndex other = m_model.index(row + 1, 0);
187         QMap<int, QVariant> seldata = m_model.itemData(sel);
188         m_model.setItemData(sel, m_model.itemData(other));
189         m_model.setItemData(other, seldata);
190         m_ui.phrasebookList->selectionModel()->setCurrentIndex(other, QItemSelectionModel::ClearAndSelect);
191     }
192 }
193 
194 QT_END_NAMESPACE
195