Russian translation. Gui fix.
[qstardict] / qstardict / dictwidget.cpp
1 /*****************************************************************************
2  * dictwidget.cpp - QStarDict, a StarDict clone written with using Qt        *
3  * Copyright (C) 2007 Alexander Rodin                                        *
4  *                                                                           *
5  * This program is free software; you can redistribute it and/or modify      *
6  * it under the terms of the GNU General Public License as published by      *
7  * the Free Software Foundation; either version 2 of the License, or         *
8  * (at your option) any later version.                                       *
9  *                                                                           *
10  * This program is distributed in the hope that it will be useful,           *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
13  * GNU General Public License for more details.                              *
14  *                                                                           *
15  * You should have received a copy of the GNU General Public License along   *
16  * with this program; if not, write to the Free Software Foundation, Inc.,   *
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.               *
18  *****************************************************************************/
19
20 #include "dictwidget.h"
21
22 #include <QScrollBar>
23 #include <QVBoxLayout>
24 #include <QToolBar>
25 #include <QAction>
26 #include <QIcon>
27 #include <QFileDialog>
28 #include <QDir>
29 #include <QFile>
30 #include <QTextStream>
31 #include <QMessageBox>
32 #include <QMouseEvent>
33 #include <QTextDocument>
34 #ifndef MAEMO
35 #include <QPrinter>
36 #include <QPrintDialog>
37 #endif // MAEMO
38 #include "application.h"
39 #include "dictbrowser.h"
40 #include "speaker.h"
41
42 namespace
43 {
44 class DictWidgetToolbar: public QToolBar
45 {
46     public:
47         DictWidgetToolbar(QWidget *parent = 0)
48             : QToolBar(parent)
49         { }
50
51     protected:
52         virtual void mouseDoubleClickEvent(QMouseEvent *event)
53         {
54             if (! actionAt(event->pos()))
55                 QToolBar::mouseDoubleClickEvent(event);
56         }
57 };
58 }
59
60 namespace QStarDict
61 {
62
63 DictWidget::DictWidget(QWidget *parent, Qt::WindowFlags f)
64     : QFrame(parent, f)
65 {
66     m_translationView = new DictBrowser(this);
67     setFrameStyle(m_translationView->frameStyle());
68     m_translationView->setFrameStyle(QFrame::NoFrame);
69     m_translationView->verticalScrollBar()->setCursor(Qt::ArrowCursor);
70     m_translationView->horizontalScrollBar()->setCursor(Qt::ArrowCursor);
71     m_translationView->setOpenExternalLinks(true);
72     connect(m_translationView, SIGNAL(sourceChanged(const QUrl&)), SLOT(on_translationView_sourceChanged(const QUrl&)));
73
74     m_toolBar = new DictWidgetToolbar(this);
75     m_toolBar->setMouseTracking(true);
76
77     QAction *actionBackward = m_toolBar->addAction(QIcon(":/icons/go-previous.png"), tr("Previous"),
78             m_translationView, SLOT(backward()));
79     actionBackward->setDisabled(true);
80     connect(m_translationView, SIGNAL(backwardAvailable(bool)), actionBackward, SLOT(setEnabled(bool)));
81
82     QAction *actionForward = m_toolBar->addAction(QIcon(":/icons/go-next.png"), tr("Next"),
83             m_translationView, SLOT(forward()));
84     actionForward->setDisabled(true);
85     connect(m_translationView, SIGNAL(forwardAvailable(bool)), actionForward, SLOT(setEnabled(bool)));
86
87     QAction *actionSaveToFile = m_toolBar->addAction(QIcon(":/icons/document-save-as.png"), tr("&Save to file"),
88             this, SLOT(saveToFile()));
89
90     QFont font;
91     font.setPointSize(16);
92     actionBackward->setFont(font);
93     actionForward->setFont(font);
94     actionSaveToFile->setFont(font);
95
96     #ifndef MAEMO
97     QAction *actionPrint = m_toolBar->addAction(QIcon(":/icons/document-print.png"), tr("Prin&t translation"),
98             this, SLOT(print()));
99     actionPrint->setFont(font);
100     #endif // MAEMO
101
102     QAction *actionSpeak = m_toolBar->addAction(QIcon(":/icons/speaker.png"), tr("Speak &word"),
103             this, SLOT(speak()));
104     actionSpeak->setFont(font);
105     QSize toolBarSize;
106     toolBarSize.setWidth(70);
107     toolBarSize.setHeight(50);
108     m_toolBar->setIconSize(toolBarSize);
109
110     QVBoxLayout *layout = new QVBoxLayout(this);
111     layout->setMargin(0);
112     layout->setSpacing(0);
113     layout->addWidget(m_toolBar);
114     layout->addWidget(m_translationView);
115     setLayout(layout);
116 }
117
118 void DictWidget::toggleToolBar(bool CheckedState)
119 {
120     CheckedState ? this->m_toolBar->show() : this->m_toolBar->hide();
121     //m_toolBar->setVisible(CheckedState);
122 }
123
124 void DictWidget::translate(const QString &str)
125 {
126     m_translationView->setSource(QUrl("qstardict:" + str));
127 }
128
129 void DictWidget::on_translationView_sourceChanged(const QUrl &name)
130 {
131     emit wordTranslated(name.toString(QUrl::RemoveScheme));
132 }
133
134 void DictWidget::saveToFile()
135 {
136         static QDir dir( QDir::homePath() ); //added by Frank
137         static QString filter(tr("Text files (*.txt)")); //added by Frank
138         
139     QFileDialog dialog(this, tr("Save translation"),
140                        dir.path(), filter); //updated by Frank
141         dialog.selectFile(translatedWord());//added by Frank
142     dialog.setNameFilters(QStringList() << tr("HTML files (*.html, *.htm)") << tr("Text files (*.txt)"));//updated by Frank
143     dialog.selectNameFilter(filter); //added by Frank
144
145         if (dialog.exec() && dialog.selectedFiles().size())
146     {
147         QString fileName = dialog.selectedFiles().first();
148         /*QString*/ filter = dialog.selectedFilter();//updated by Frank
149                 dir = dialog.directory(); //added by Frank
150         if (filter == tr("HTML files (*.html, *.htm)") && 
151             ! (fileName.endsWith(".html", Qt::CaseInsensitive) || fileName.endsWith(".htm", Qt::CaseInsensitive)))
152             fileName += ".html";
153         else if (filter == tr("Text files (*.txt)") && ! fileName.endsWith(".txt", Qt::CaseInsensitive))
154             fileName += ".txt";
155
156         QFile outputFile(fileName);
157         if (! outputFile.open(QIODevice::WriteOnly | QIODevice::Text))
158         {
159             QMessageBox::warning(this, tr("Error"),
160                                  tr("Cannot save translation as %1").arg(fileName));
161             return;
162         }
163         QTextStream outputStream(&outputFile);
164         if (filter == tr("HTML files (*.html, *.htm)"))
165             outputStream << m_translationView->document()->toHtml("UTF-8");
166         else
167             outputStream << m_translationView->toPlainText();
168     }
169 }
170
171 void DictWidget::speak()
172 {
173     Application::instance()->speaker()->speak(translatedWord());
174 }
175
176 #ifndef MAEMO
177 void DictWidget::print()
178 {
179     QPrinter printer(QPrinter::HighResolution);
180     QPrintDialog dialog(&printer, this);
181     if (dialog.exec() == QDialog::Accepted)
182         m_translationView->print(&printer);
183 }
184 #endif // MAEMO
185
186 void DictWidget::setDefaultStyleSheet(const QString &css)
187 {
188     m_translationView->document()->setDefaultStyleSheet(css);
189     m_translationView->reload();
190 }
191
192 }
193
194 // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc
195