12cd2e14a24c9aabde6f56679f558744e1dfaf69
[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::translate(const QString &str)
119 {
120     m_translationView->setSource(QUrl("qstardict:" + str));
121 }
122
123 void DictWidget::on_translationView_sourceChanged(const QUrl &name)
124 {
125     emit wordTranslated(name.toString(QUrl::RemoveScheme));
126 }
127
128 void DictWidget::saveToFile()
129 {
130         static QDir dir( QDir::homePath() ); //added by Frank
131         static QString filter(tr("Text files (*.txt)")); //added by Frank
132         
133     QFileDialog dialog(this, tr("Save translation"),
134                        dir.path(), filter); //updated by Frank
135         dialog.selectFile(translatedWord());//added by Frank
136     dialog.setNameFilters(QStringList() << tr("HTML files (*.html *.htm)") << tr("Text files (*.txt)"));//updated by Frank
137     dialog.selectNameFilter(filter); //added by Frank
138
139         if (dialog.exec() && dialog.selectedFiles().size())
140     {
141         QString fileName = dialog.selectedFiles().first();
142         /*QString*/ filter = dialog.selectedFilter();//updated by Frank
143                 dir = dialog.directory(); //added by Frank
144         if (filter == tr("HTML files (*.html, *.htm)") && 
145             ! (fileName.endsWith(".html", Qt::CaseInsensitive) || fileName.endsWith(".htm", Qt::CaseInsensitive)))
146             fileName += ".html";
147         else if (filter == tr("Text files (*.txt)") && ! fileName.endsWith(".txt", Qt::CaseInsensitive))
148             fileName += ".txt";
149
150         QFile outputFile(fileName);
151         if (! outputFile.open(QIODevice::WriteOnly | QIODevice::Text))
152         {
153             QMessageBox::warning(this, tr("Error"),
154                                  tr("Cannot save translation as %1").arg(fileName));
155             return;
156         }
157         QTextStream outputStream(&outputFile);
158         if (filter == tr("HTML files (*.html, *.htm)"))
159             outputStream << m_translationView->document()->toHtml("UTF-8");
160         else
161             outputStream << m_translationView->toPlainText();
162     }
163 }
164
165 void DictWidget::speak()
166 {
167     Application::instance()->speaker()->speak(translatedWord());
168 }
169
170 #ifndef MAEMO
171 void DictWidget::print()
172 {
173     QPrinter printer(QPrinter::HighResolution);
174     QPrintDialog dialog(&printer, this);
175     if (dialog.exec() == QDialog::Accepted)
176         m_translationView->print(&printer);
177 }
178 #endif // MAEMO
179
180 void DictWidget::setDefaultStyleSheet(const QString &css)
181 {
182     m_translationView->document()->setDefaultStyleSheet(css);
183     m_translationView->reload();
184 }
185
186 }
187
188 // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc
189