X-Git-Url: https://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Fgui%2FPreferencesDialog.cpp;h=9c5af008ec763d7151e30b8ba7db1b90e1f7fe30;hb=HEAD;hp=3742c41d70a3852eca5d9f84269b9f262858c7b5;hpb=d27c2db830de9a65d6a419479ee598cbf3697746;p=qtrapids diff --git a/src/gui/PreferencesDialog.cpp b/src/gui/PreferencesDialog.cpp index 3742c41..9c5af00 100644 --- a/src/gui/PreferencesDialog.cpp +++ b/src/gui/PreferencesDialog.cpp @@ -1,11 +1,9 @@ /*************************************************************************** - * Copyright (C) 2009 by Lassi Väätämöinen * - * lassi.vaatamoinen@ixonos.com * + * Copyright (C) 2010 by Ixonos Plc * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * + * the Free Software Foundation; version 2 of the License. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * @@ -22,49 +20,75 @@ #include #include #include +#include #include #include #include +#include "QBittorrentSession.h" #include "PreferencesDialog.h" -PreferencesDialog::PreferencesDialog(QWidget* parent, Qt::WindowFlags f) : - QDialog(parent, f), // Superclass - dirLineEdit_(NULL), - dialogButtons_(NULL), - settings_() +PreferencesDialog::PreferencesDialog(QWidget* parent, Qt::WindowFlags f, + qtrapids::QBittorrentSession *const btSession) : + QDialog(parent, f), // Superclass + dirLineEdit_(NULL), + dialogButtons_(NULL), + uploadRateSpinBox_(NULL), + downloadRateSpinBox_(NULL), + btSession_(btSession), + settings_() { - setWindowTitle("Preferences"); - - QBoxLayout *verticalBox = new QBoxLayout(QBoxLayout::TopToBottom); - QBoxLayout *horizontalBox1 = new QBoxLayout(QBoxLayout::LeftToRight); - setLayout(verticalBox); - verticalBox->addLayout(horizontalBox1); - - QLabel *dirLabel = new QLabel(tr("Download directory: ")); - dirLineEdit_ = new QLineEdit(this); - QPushButton *browseDirButton = new QPushButton(tr("Browse..")); - - horizontalBox1->addWidget(dirLabel); - horizontalBox1->addWidget(dirLineEdit_); - horizontalBox1->addWidget(browseDirButton); - - connect(browseDirButton, SIGNAL(clicked()), - this, SLOT(on_browseDirButtonClicked())); - - - dialogButtons_ = new QDialogButtonBox(this); - dialogButtons_->setStandardButtons(QDialogButtonBox::Ok - | QDialogButtonBox::Apply - | QDialogButtonBox::Cancel); - - verticalBox->addWidget(dialogButtons_); - - connect(dialogButtons_, SIGNAL(clicked(QAbstractButton*)), - this, SLOT(on_buttonClicked(QAbstractButton*))); - - // Set saved preference values to fields. - ReadSettings(); + setWindowTitle("Preferences"); + + QBoxLayout *verticalBox = new QBoxLayout(QBoxLayout::TopToBottom); + //QBoxLayout *horizontalBox1 = new QBoxLayout(QBoxLayout::LeftToRight); + QGridLayout *grid = new QGridLayout; + setLayout(verticalBox); + verticalBox->addLayout(grid); + + QLabel *dirLabel = new QLabel(tr("Download directory: ")); + dirLineEdit_ = new QLineEdit(this); + QPushButton *browseDirButton = new QPushButton(tr("Browse..")); + + QLabel *uploadLabel = new QLabel(tr("Max. upload rate: ")); + QLabel *downloadLabel = new QLabel(tr("Max. download rate: ")); + uploadRateSpinBox_ = new QSpinBox(this); + downloadRateSpinBox_ = new QSpinBox(this); + + grid->addWidget(dirLabel, 0, 0); + grid->addWidget(dirLineEdit_, 0, 1); + grid->addWidget(browseDirButton, 0, 2); + + grid->addWidget(uploadLabel, 1, 0); + grid->addWidget(uploadRateSpinBox_, 1, 1); + grid->addWidget(downloadLabel, 2, 0); + grid->addWidget(downloadRateSpinBox_, 2, 1); + + uploadRateSpinBox_->setRange(0, 1000); + uploadRateSpinBox_->setSuffix(" kB/s"); + downloadRateSpinBox_->setRange(0, 1000); + downloadRateSpinBox_->setSuffix(" kB/s"); + +// horizontalBox1->addWidget(dirLabel); +// horizontalBox1->addWidget(dirLineEdit_); +// horizontalBox1->addWidget(browseDirButton); + + connect(browseDirButton, SIGNAL(clicked()), + this, SLOT(on_browseDirButtonClicked())); + + + dialogButtons_ = new QDialogButtonBox(this); + dialogButtons_->setStandardButtons(QDialogButtonBox::Ok + | QDialogButtonBox::Apply + | QDialogButtonBox::Cancel); + + verticalBox->addWidget(dialogButtons_); + + connect(dialogButtons_, SIGNAL(clicked(QAbstractButton*)), + this, SLOT(on_buttonClicked(QAbstractButton*))); + + // Set saved preference values to fields. + ReadSettings(); } @@ -76,68 +100,82 @@ PreferencesDialog::~PreferencesDialog() // ======================== SLOTS ======================== void PreferencesDialog::on_browseDirButtonClicked() { - QFileDialog *dialog - = new QFileDialog(this, "Download directory", - QString(), tr("Torrent files (*.torrent)")); + QFileDialog *dialog + = new QFileDialog(this, "Download directory", + QString(), tr("Torrent files (*.torrent)")); - dialog->setFileMode(QFileDialog::Directory); - dialog->setOption(QFileDialog::ShowDirsOnly, true); + dialog->setFileMode(QFileDialog::Directory); + dialog->setOption(QFileDialog::ShowDirsOnly, true); - connect(dialog, SIGNAL(fileSelected(const QString&)), - this, SLOT(on_downloadDirectorySelected(const QString&))); + connect(dialog, SIGNAL(fileSelected(const QString&)), + this, SLOT(on_downloadDirectorySelected(const QString&))); - dialog->show(); + dialog->show(); } void PreferencesDialog::on_buttonClicked(QAbstractButton* button) { - switch (dialogButtons_->buttonRole ( button ) ) - { - case QDialogButtonBox::AcceptRole : - qDebug() << "PreferencesDialog: OK"; - WriteSettings(); - close(); - break; - case QDialogButtonBox::ApplyRole : - qDebug() << "PreferencesDialog: APPLY"; - WriteSettings(); - break; - case QDialogButtonBox::RejectRole : - qDebug() << "PreferencesDialog: CANCEL"; - close(); - break; - default: - return; - } + switch (dialogButtons_->buttonRole ( button ) ) { + case QDialogButtonBox::AcceptRole : + qDebug() << "PreferencesDialog: OK"; + WriteSettings(); + done(QDialog::Accepted); + break; + case QDialogButtonBox::ApplyRole : + qDebug() << "PreferencesDialog: APPLY"; + WriteSettings(); + break; + case QDialogButtonBox::RejectRole : + qDebug() << "PreferencesDialog: CANCEL"; + done(QDialog::Rejected); + break; + default: + return; + } } void PreferencesDialog::on_downloadDirectorySelected(const QString& directory) { - qDebug() << "PreferencesDialog::on_downloadDirectorySelected(): " << directory; - // Torrent filename empty, do nothing. - if (directory == "") - return; + qDebug() << "PreferencesDialog::on_downloadDirectorySelected(): " << directory; + // Torrent filename empty, do nothing. + if (directory == "") + return; - dirLineEdit_->insert(directory); + dirLineEdit_->clear(); + dirLineEdit_->insert(directory); - /// @todo check that user has privileges to write to this directory. + /// @todo check that user has privileges to write to this directory. } // ========================= Private functions ========================== void PreferencesDialog::WriteSettings() { - settings_.setValue("download/directory", dirLineEdit_->text()); - - // NOTE: We might need to call QSettigns::sync() here to instantly write settings. - // settings are written also by QSettings() destructor and by event loop at regular interval. + int ulRate = 1000*uploadRateSpinBox_->value(); + int dlRate = 1000*downloadRateSpinBox_->value(); + + settings_.setValue("download/directory", dirLineEdit_->text()); + settings_.setValue("network/uploadRate", ulRate); + settings_.setValue("network/downloadRate", dlRate); + + // NOTE: We might need to call QSettigns::sync() here to instantly write settings. + // settings are written also by QSettings() destructor and by event loop at regular interval. + + // If session pointer was given, apply settings immediately. + if (btSession_) { + btSession_->setUploadRateLimit(ulRate); + btSession_->setDownloadRateLimit(dlRate); + } + } void PreferencesDialog::ReadSettings() { - dirLineEdit_->insert(settings_.value("download/directory").toString()); - - // NOTE: We might need to call QSettigns::sync() here to instantly write settings. - // settings are written also by QSettings() destructor and by event loop at regular interval. + dirLineEdit_->insert(settings_.value("download/directory").toString()); + uploadRateSpinBox_->setValue(settings_.value("network/uploadRate").toInt()/1000); + downloadRateSpinBox_->setValue(settings_.value("network/downloadRate").toInt()/1000); + + // NOTE: We might need to call QSettigns::sync() here to instantly write settings. + // settings are written also by QSettings() destructor and by event loop at regular interval. }