1 /***************************************************************************
2 *   Copyright (C) 2007 by BOP                                             *
3 *                                                                         *
4 *   This program is free software; you can redistribute it and/or modify  *
5 *   it under the terms of the GNU General Public License as published by  *
6 *   the Free Software Foundation; either version 2 of the License, or     *
7 *   (at your option) any later version.                                   *
8 *                                                                         *
9 *   This program is distributed in the hope that it will be useful,       *
10 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12 *   GNU General Public License for more details.                          *
13 *                                                                         *
14 *   You should have received a copy of the GNU General Public License     *
15 *   along with this program; if not, write to the                         *
16 *   Free Software Foundation, Inc.,                                       *
17 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18 ***************************************************************************/
19 
20 #include "ssheetsetting.h"
21 
22 #include <QDialogButtonBox>
23 #include <QFileDialog>
24 #include <QPushButton>
25 #include <QSplitter>
26 #include <QTextStream>
27 #include <QVBoxLayout>
28 
29 const char *view1 =
30     "<html>\n"
31     "<head>\n"
32     "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n"
33     "<style type=\"text/css\">\n";
34 
35 const char *view2 =
36     "</style>\n"
37     "</head>\n"
38     "<body>\n";
39 
40 const char *view3 =
41     "</body></html>";
42 
43 
SSheetSetting(const QString & current,const QString & defsheet,bool dic,QWidget * parent)44 SSheetSetting::SSheetSetting(const QString &current, const QString &defsheet,
45                              bool dic, QWidget *parent)
46     : QDialog(parent), initialSheet(current), defaultSheet(defsheet)
47 {
48     setWindowTitle(tr("Browser style sheet setting"));
49 
50     QFile file((dic) ? ":/data/dict-style.html" : ":/data/book-style.html");
51     file.open(QIODevice::ReadOnly | QIODevice::Text);
52     QTextStream in(&file);
53     testText = in.readAll();
54 
55     QVBoxLayout *v = new QVBoxLayout;
56     QSplitter *splitter = new QSplitter(this);
57     edit = new QTextEdit();
58     edit->setPlainText(current);
59     view = new QTextEdit();
60     view->setReadOnly(true);
61     view->setHtml(setViewText());
62     splitter->addWidget(edit);
63     splitter->addWidget(view);
64     splitter->setStretchFactor(splitter->indexOf(view),  1);
65     v->addWidget(splitter);
66     v->setStretchFactor(splitter, 1);
67 
68     QDialogButtonBox *bBox = new QDialogButtonBox(
69         QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
70         QDialogButtonBox::RestoreDefaults | QDialogButtonBox::Reset);
71     QPushButton *save = bBox->addButton(tr("Save to file..."),
72                                         QDialogButtonBox::ActionRole);
73     QPushButton *load = bBox->addButton(tr("Load from file..."),
74                                         QDialogButtonBox::ActionRole);
75     QPushButton *aply = bBox->addButton(tr("Test"),
76                                         QDialogButtonBox::ActionRole);
77     connect(bBox, SIGNAL(accepted()), SLOT(accept()));
78     connect(bBox, SIGNAL(rejected()), SLOT(reject()));
79     connect(bBox->button(QDialogButtonBox::RestoreDefaults),
80             SIGNAL(clicked()), SLOT(defaultReset()));
81     connect(bBox->button(QDialogButtonBox::Reset), SIGNAL(clicked()),
82             SLOT(reset()));
83     connect(save, SIGNAL(clicked()), SLOT(save()));
84     connect(load, SIGNAL(clicked()), SLOT(load()));
85     connect(aply, SIGNAL(clicked()), SLOT(apply()));
86     v->addWidget(bBox);
87 
88     setLayout(v);
89 }
90 
91 // private slot:
92 //
93 
save()94 void SSheetSetting::save()
95 {
96     QString file = QFileDialog::getSaveFileName(this,
97                                                 tr("Save style sheet"),
98                                                 QDir::homePath());
99 
100     if (file.isEmpty()) {
101         return;
102     }
103     QFile f(file);
104     if (!f.open(QIODevice::WriteOnly | QIODevice::Text)) {
105         return;
106     }
107     QTextStream out(&f);
108     out << edit->toPlainText();
109 }
110 
load()111 void SSheetSetting::load()
112 {
113     QString file = QFileDialog::getOpenFileName(this,
114                                                 tr("Load style sheet"),
115                                                 QDir::homePath());
116 
117     if (file.isEmpty()) {
118         return;
119     }
120     QFile f(file);
121     if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
122         return;
123     }
124     QTextStream in(&f);
125     edit->setPlainText(in.readAll());
126     setViewText();
127 }
128 
129 
130 
setViewText()131 QString SSheetSetting::setViewText()
132 {
133     return view1 + edit->toPlainText() + view2 + testText + view3;
134 }
135 
SSheetOptSetting(const QString & current,const QString & defsheet,QWidget * parent)136 SSheetOptSetting::SSheetOptSetting(const QString &current,
137                                    const QString &defsheet, QWidget *parent)
138     : QDialog(parent), initialSheet(current), defaultSheet(defsheet)
139 {
140     setWindowTitle(tr("Style sheet setting"));
141 
142     QVBoxLayout *v = new QVBoxLayout;
143     edit = new QTextEdit();
144     edit->setPlainText(current);
145 
146     QDialogButtonBox *bBox = new QDialogButtonBox(
147         QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
148         QDialogButtonBox::RestoreDefaults | QDialogButtonBox::Reset);
149     connect(bBox, SIGNAL(accepted()), SLOT(accept()));
150     connect(bBox, SIGNAL(rejected()), SLOT(reject()));
151     connect(bBox->button(QDialogButtonBox::RestoreDefaults), SIGNAL(clicked()),
152             SLOT(defaultReset()));
153     connect(bBox->button(QDialogButtonBox::Reset), SIGNAL(clicked()),
154             SLOT(reset()));
155     v->addWidget(edit);
156     v->setStretchFactor(edit, 1);
157     v->addWidget(bBox);
158 
159     setLayout(v);
160 }
161