Back to index

scribus-ng  1.3.4.dfsg+svn20071115
swprefsgui.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 #include "swprefsgui.h"
00008 #include "version.h"
00009 #include "scpaths.h"
00010 #include "commonstrings.h"
00011 
00012 #include <qvariant.h>
00013 #include <qwidget.h>
00014 #include <qlayout.h>
00015 #include <qpushbutton.h>
00016 #include <qlabel.h>
00017 #include <qfile.h>
00018 #include <qdir.h>
00019 #include <qtooltip.h>
00020 #include <qtextcodec.h>
00021 
00022 #include "scmessagebox.h"
00023 
00024 SWPrefsGui::SWPrefsGui(QWidget* parent )
00025        : PrefsPanel(parent, "SWPrefsGui")
00026 {
00027        SWPrefsGuiLayout = new QGridLayout(this, 1, 1, 11, 6, "SWPrefsGuiLayout");
00028 
00029        editLayout = new QVBoxLayout(0, 0, 6, "editLayout");
00030 
00031        titleLabel = new QLabel(this, "titleLabel");
00032        editLayout->addWidget(titleLabel);
00033        cfgEdit = new QTextEdit(this, "cfgEdit");
00034        editLayout->addWidget(cfgEdit);
00035 
00036        buttonLayout = new QHBoxLayout(0, 0, 6, "buttonLayout");
00037        buttonSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
00038        buttonLayout->addItem(buttonSpacer);
00039 
00040        okButton = new QPushButton(this, "okButton");
00041        buttonLayout->addWidget(okButton);
00042 
00043        resetButton = new QPushButton(this, "resetButton");
00044        buttonLayout->addWidget(resetButton);
00045        editLayout->addLayout(buttonLayout);
00046 
00047        SWPrefsGuiLayout->addLayout(editLayout, 0, 0);
00048        languageChange();
00049        resize(QSize(362, 359).expandedTo(minimumSizeHint()));
00050        clearWState(WState_Polished);
00051 
00052        // defaults
00053        if (QFile::exists(RC_PATH_USR))
00054        {
00055               titleLabel->setText( tr("User settings"));
00056               loadCfgFile(RC_PATH_USR);
00057        }
00058        else
00059        {
00060               titleLabel->setText( tr("System wide configuration"));
00061               resetButton->setEnabled(false);
00062               loadCfgFile(RC_PATH);
00063        }
00064        okButton->setEnabled(false);
00065        SWSyntaxHighlighter *sxHigh = new SWSyntaxHighlighter(cfgEdit);
00066        //remove that unused warning!
00067        sxHigh->currentParagraph();
00068 
00069        // signals
00070        connect(okButton, SIGNAL(clicked()), this, SLOT(okButton_pressed()));
00071        connect(resetButton, SIGNAL(clicked()), this, SLOT(resetButton_pressed()));
00072        connect(cfgEdit, SIGNAL(textChanged()), this, SLOT(cfgEdit_changed()));
00073 }
00074 
00075 SWPrefsGui::~SWPrefsGui()
00076 {
00077        //delete the highlighter
00078        delete cfgEdit->syntaxHighlighter();
00079 }
00080 
00081 /*
00082  *  Sets the strings of the subwidgets using the current
00083  *  language.
00084  */
00085 void SWPrefsGui::languageChange()
00086 {
00087        okButton->setText( tr("&Save"));
00088        resetButton->setText( tr("&Reset"));
00089        // tooltips
00090        QToolTip::add(okButton, tr("Save user configuration"));
00091        QToolTip::add(resetButton, "<qt>" + tr("Reload system wide configuration and remove user defined one") + "</qt>");
00092        QToolTip::add(cfgEdit, "<qt>" + tr("Edit custom configuration. If you save it, it will be used over system wide configuration") + "</qt>");
00093 }
00094 
00095 void SWPrefsGui::apply()
00096 {
00097        if (okButton->isEnabled())
00098               okButton_pressed();
00099 }
00100 
00101 void SWPrefsGui::okButton_pressed()
00102 {
00103        if (QFile::exists(RC_PATH_USR))
00104        {
00105               if ((ScMessageBox::warning(this, tr("Short Words"),
00106                             "<qt>" + tr("User configuration exists elready. "
00107                                           "Do you really want to overwrite it?") + "</qt>",
00108                             CommonStrings::trYes,
00109                             CommonStrings::trNo, 0, 0, 1)
00110                      ) == 1)
00111                      return;
00112        }
00113        QFile f(RC_PATH_USR);
00114        if (!f.open(IO_WriteOnly))
00115        {
00116               QMessageBox::warning(this, tr("Short Words"),
00117                       "<qt>" + tr("Cannot write file %1.").arg(RC_PATH_USR) + "</qt>",
00118                       CommonStrings::tr_OK);
00119        }
00120        QTextStream stream(&f);
00121        stream.setCodec(QTextCodec::codecForName("utf8"));
00122        stream << cfgEdit->text();
00123        f.close();
00124        titleLabel->setText( tr("User settings saved"));
00125        okButton->setEnabled(false);
00126 }
00127 
00128 void SWPrefsGui::resetButton_pressed()
00129 {
00130        loadCfgFile(RC_PATH);
00131        QDir d;
00132        d.remove(RC_PATH_USR);
00133        okButton->setEnabled(false);
00134        resetButton->setEnabled(false);
00135        titleLabel->setText( tr("System wide configuration reloaded"));
00136 }
00137 
00138 void SWPrefsGui::cfgEdit_changed()
00139 {
00140        resetButton->setEnabled(true);
00141        okButton->setEnabled(true);
00142 }
00143 
00144 bool SWPrefsGui::loadCfgFile(QString filename)
00145 {
00146        QFile f(filename);
00147        if (!f.open(IO_ReadOnly))
00148        {
00149               titleLabel->setText( tr("Cannot open file %1").arg(f.name()));
00150               return false;
00151        }
00152        cfgEdit->clear();
00153        QTextStream stream(&f);
00154        stream.setCodec(QTextCodec::codecForName("utf8"));
00155        while (!stream.atEnd())
00156               cfgEdit->append(stream.readLine());
00157        f.close();
00158        return true;
00159 }
00160 
00161 
00162 /*
00163  * Syntax highlighting
00164  */
00165 SWSyntaxHighlighter::SWSyntaxHighlighter(QTextEdit *textEdit)
00166        : QSyntaxHighlighter(textEdit)
00167 {
00168 }
00169 
00170 int SWSyntaxHighlighter::highlightParagraph(const QString &text, int)
00171 {
00172        // position in the text
00173        if (text[0] == '#')
00174        {
00175               QFont f(textEdit()->currentFont());
00176               f.setItalic(true);
00177               setFormat(0, text.length(), f, QColor(Qt::gray));
00178        }
00179        return 0;
00180 }
00181 
00182 #include "swprefsgui.moc"