Commit 031acae3 authored by ck's avatar ck
Browse files

Maemo: Complete options page.

parent 28e3ba5b
...@@ -54,7 +54,7 @@ namespace { ...@@ -54,7 +54,7 @@ namespace {
}; };
MaemoDeviceConfigurations::DeviceConfig::DeviceConfig(const QString &name) MaemoDeviceConfigurations::DeviceConfig::DeviceConfig(const QString &name)
: name(name), type(Physical) : name(name), type(Physical), port(22), timeout(30)
{ {
} }
...@@ -97,8 +97,10 @@ void MaemoDeviceConfigurations::save() ...@@ -97,8 +97,10 @@ void MaemoDeviceConfigurations::save()
QSettings *settings = Core::ICore::instance()->settings(); QSettings *settings = Core::ICore::instance()->settings();
settings->beginGroup(SettingsGroup); settings->beginGroup(SettingsGroup);
settings->beginWriteArray(ConfigListKey, m_devConfigs.count()); settings->beginWriteArray(ConfigListKey, m_devConfigs.count());
foreach (const DeviceConfig &devConfig, m_devConfigs) for (int i = 0; i < m_devConfigs.count(); ++i) {
devConfig.save(*settings); settings->setArrayIndex(i);
m_devConfigs.at(i).save(*settings);
}
settings->endArray(); settings->endArray();
settings->endGroup(); settings->endGroup();
} }
...@@ -114,8 +116,10 @@ void MaemoDeviceConfigurations::load() ...@@ -114,8 +116,10 @@ void MaemoDeviceConfigurations::load()
QSettings *settings = Core::ICore::instance()->settings(); QSettings *settings = Core::ICore::instance()->settings();
settings->beginGroup(SettingsGroup); settings->beginGroup(SettingsGroup);
int count = settings->beginReadArray(ConfigListKey); int count = settings->beginReadArray(ConfigListKey);
for (int i = 0; i < count; ++i) for (int i = 0; i < count; ++i) {
settings->setArrayIndex(i);
m_devConfigs.append(DeviceConfig(*settings)); m_devConfigs.append(DeviceConfig(*settings));
}
settings->endArray(); settings->endArray();
settings->endGroup(); settings->endGroup();
} }
......
...@@ -65,6 +65,18 @@ public: ...@@ -65,6 +65,18 @@ public:
int timeout; int timeout;
}; };
class DevConfMatcher
{
public:
DevConfMatcher(const QString &name) : m_name(name) {}
bool operator()(const MaemoDeviceConfigurations::DeviceConfig &devConfig)
{
return devConfig.name == m_name;
}
private:
const QString m_name;
};
static MaemoDeviceConfigurations &instance(); static MaemoDeviceConfigurations &instance();
QList<DeviceConfig> devConfigs() const { return m_devConfigs; } QList<DeviceConfig> devConfigs() const { return m_devConfigs; }
void setDevConfigs(const QList<DeviceConfig> &devConfigs); void setDevConfigs(const QList<DeviceConfig> &devConfigs);
......
...@@ -46,6 +46,8 @@ ...@@ -46,6 +46,8 @@
#include "ui_maemosettingswidget.h" #include "ui_maemosettingswidget.h"
#include "maemosettingspage.h" #include "maemosettingspage.h"
#include <QtGui/QIntValidator>
#include <algorithm> #include <algorithm>
namespace Qt4ProjectManager { namespace Qt4ProjectManager {
...@@ -54,16 +56,61 @@ namespace Internal { ...@@ -54,16 +56,61 @@ namespace Internal {
#define PAGE_ID "ZZ.Maemo Device Configurations" #define PAGE_ID "ZZ.Maemo Device Configurations"
#define PAGE_ID_TR "Maemo Device Configurations" #define PAGE_ID_TR "Maemo Device Configurations"
class DevConfMatcher bool configNameExists(const QList<MaemoDeviceConfigurations::DeviceConfig> &devConfs,
const QString &name)
{
return std::find_if(devConfs.constBegin(), devConfs.constEnd(),
MaemoDeviceConfigurations::DevConfMatcher(name)) != devConfs.constEnd();
}
class PortAndTimeoutValidator : public QIntValidator
{ {
public: public:
DevConfMatcher(const QString &name) : m_name(name) {} PortAndTimeoutValidator() : QIntValidator(0, SHRT_MAX, 0)
bool operator()(const MaemoDeviceConfigurations::DeviceConfig &devConfig)
{ {
return devConfig.name == m_name;
} }
void setValue(int oldValue) { m_oldValue = oldValue; }
virtual void fixup(QString &input) const
{
int dummy = 0;
if (validate(input, dummy) != Acceptable)
input = QString::number(m_oldValue);
}
private: private:
const QString m_name; int m_oldValue;
};
class NameValidator : public QValidator
{
public:
NameValidator(const QList<MaemoDeviceConfigurations::DeviceConfig> &devConfs)
: m_devConfs(devConfs)
{
}
void setName(const QString &name) { m_oldName = name; }
virtual State validate(QString &input, int & /* pos */) const
{
if (input.trimmed().isEmpty()
|| (input != m_oldName && configNameExists(m_devConfs, input)))
return Intermediate;
return Acceptable;
}
virtual void fixup(QString &input) const
{
int dummy = 0;
if (validate(input, dummy) != Acceptable)
input = m_oldName;
}
private:
QString m_oldName;
const QList<MaemoDeviceConfigurations::DeviceConfig> &m_devConfs;
}; };
...@@ -72,24 +119,44 @@ class MaemoSettingsWidget : public QWidget ...@@ -72,24 +119,44 @@ class MaemoSettingsWidget : public QWidget
Q_OBJECT Q_OBJECT
public: public:
MaemoSettingsWidget(QWidget *parent); MaemoSettingsWidget(QWidget *parent);
~MaemoSettingsWidget();
void saveSettings(); void saveSettings();
private slots:
void selectionChanged();
void addConfig();
void deleteConfig();
void configNameEditingFinished();
void deviceTypeChanged();
void hostNameEditingFinished();
void portEditingFinished();
void timeoutEditingFinished();
void userNameEditingFinished();
void passwordEditingFinished();
private: private:
void initGui(); void initGui();
void addConfig();
void display(const MaemoDeviceConfigurations::DeviceConfig &devConfig); void display(const MaemoDeviceConfigurations::DeviceConfig &devConfig);
MaemoDeviceConfigurations::DeviceConfig &currentConfig();
void setPortOrTimeout(const QLineEdit *lineEdit, int &confVal,
PortAndTimeoutValidator &validator);
void clearDetails();
Ui_maemoSettingsWidget *m_ui; Ui_maemoSettingsWidget *m_ui;
QList<MaemoDeviceConfigurations::DeviceConfig> m_devConfs; QList<MaemoDeviceConfigurations::DeviceConfig> m_devConfs;
PortAndTimeoutValidator m_portValidator;
PortAndTimeoutValidator m_timeoutValidator;
NameValidator m_nameValidator;
}; };
MaemoSettingsPage::MaemoSettingsPage(QObject *parent) MaemoSettingsPage::MaemoSettingsPage(QObject *parent)
: Core::IOptionsPage(parent), m_widget(0) : Core::IOptionsPage(parent)
{ {
qDebug("Creating maemo settings page");
} }
MaemoSettingsPage::~MaemoSettingsPage() MaemoSettingsPage::~MaemoSettingsPage()
{ {
qDebug("deleting maemo settings page");
} }
QString MaemoSettingsPage::id() const QString MaemoSettingsPage::id() const
...@@ -115,8 +182,6 @@ QString MaemoSettingsPage::trCategory() const ...@@ -115,8 +182,6 @@ QString MaemoSettingsPage::trCategory() const
QWidget *MaemoSettingsPage::createPage(QWidget *parent) QWidget *MaemoSettingsPage::createPage(QWidget *parent)
{ {
if (m_widget != 0)
delete m_widget;
m_widget = new MaemoSettingsWidget(parent); m_widget = new MaemoSettingsWidget(parent);
return m_widget; return m_widget;
} }
...@@ -134,15 +199,25 @@ void MaemoSettingsPage::finish() ...@@ -134,15 +199,25 @@ void MaemoSettingsPage::finish()
MaemoSettingsWidget::MaemoSettingsWidget(QWidget *parent) MaemoSettingsWidget::MaemoSettingsWidget(QWidget *parent)
: QWidget(parent), : QWidget(parent),
m_ui(new Ui_maemoSettingsWidget), m_ui(new Ui_maemoSettingsWidget),
m_devConfs(MaemoDeviceConfigurations::instance().devConfigs()) m_devConfs(MaemoDeviceConfigurations::instance().devConfigs()),
m_nameValidator(m_devConfs)
{ {
qDebug("creating maemo settings widget");
initGui(); initGui();
} }
MaemoSettingsWidget::~MaemoSettingsWidget()
{
qDebug("Deleting maemo settings widget");
}
void MaemoSettingsWidget::initGui() void MaemoSettingsWidget::initGui()
{ {
m_ui->setupUi(this); m_ui->setupUi(this);
m_ui->nameLineEdit->setValidator(&m_nameValidator);
m_ui->portLineEdit->setValidator(&m_portValidator);
m_ui->timeoutLineEdit->setValidator(&m_timeoutValidator);
foreach(const MaemoDeviceConfigurations::DeviceConfig &devConf, m_devConfs) foreach(const MaemoDeviceConfigurations::DeviceConfig &devConf, m_devConfs)
m_ui->configListWidget->addItem(devConf.name); m_ui->configListWidget->addItem(devConf.name);
} }
...@@ -155,14 +230,32 @@ void MaemoSettingsWidget::addConfig() ...@@ -155,14 +230,32 @@ void MaemoSettingsWidget::addConfig()
bool isUnique = false; bool isUnique = false;
do { do {
newName = prefix + QString::number(suffix++); newName = prefix + QString::number(suffix++);
isUnique = std::find_if(m_devConfs.constBegin(), m_devConfs.constEnd(), isUnique = !configNameExists(m_devConfs, newName);
DevConfMatcher(newName)) == m_devConfs.constEnd();
} while (!isUnique); } while (!isUnique);
m_devConfs.append(MaemoDeviceConfigurations::DeviceConfig(newName)); m_devConfs.append(MaemoDeviceConfigurations::DeviceConfig(newName));
m_ui->configListWidget->addItem(newName); m_ui->configListWidget->addItem(newName);
// TODO: select and display new item (selection should automatically lead to display) m_ui->configListWidget->setCurrentRow(m_ui->configListWidget->count() - 1);
// also mark configuration name to suggest overwriting m_ui->nameLineEdit->selectAll();
m_ui->removeConfigButton->setEnabled(true);
m_ui->nameLineEdit->setFocus();
}
void MaemoSettingsWidget::deleteConfig()
{
const QList<QListWidgetItem *> &selectedItems =
m_ui->configListWidget->selectedItems();
if (selectedItems.isEmpty())
return;
QListWidgetItem *item = selectedItems.first();
const int selectedRow = m_ui->configListWidget->row(item);
m_devConfs.removeAt(selectedRow);
disconnect(m_ui->configListWidget, SIGNAL(itemSelectionChanged()), 0, 0);
delete m_ui->configListWidget->takeItem(selectedRow);
connect(m_ui->configListWidget, SIGNAL(itemSelectionChanged()),
this, SLOT(selectionChanged()));
Q_ASSERT(m_ui->configListWidget->count() == m_devConfs.count());
selectionChanged();
} }
void MaemoSettingsWidget::display(const MaemoDeviceConfigurations::DeviceConfig &devConfig) void MaemoSettingsWidget::display(const MaemoDeviceConfigurations::DeviceConfig &devConfig)
...@@ -178,6 +271,10 @@ void MaemoSettingsWidget::display(const MaemoDeviceConfigurations::DeviceConfig ...@@ -178,6 +271,10 @@ void MaemoSettingsWidget::display(const MaemoDeviceConfigurations::DeviceConfig
m_ui->userLineEdit->setText(devConfig.uname); m_ui->userLineEdit->setText(devConfig.uname);
m_ui->pwdLineEdit->setText(devConfig.pwd); m_ui->pwdLineEdit->setText(devConfig.pwd);
m_ui->detailsWidget->setEnabled(true); m_ui->detailsWidget->setEnabled(true);
m_nameValidator.setName(devConfig.name);
m_portValidator.setValue(devConfig.port);
m_timeoutValidator.setValue(devConfig.timeout);
m_ui->detailsWidget->setEnabled(true);
} }
void MaemoSettingsWidget::saveSettings() void MaemoSettingsWidget::saveSettings()
...@@ -185,6 +282,95 @@ void MaemoSettingsWidget::saveSettings() ...@@ -185,6 +282,95 @@ void MaemoSettingsWidget::saveSettings()
MaemoDeviceConfigurations::instance().setDevConfigs(m_devConfs); MaemoDeviceConfigurations::instance().setDevConfigs(m_devConfs);
} }
MaemoDeviceConfigurations::DeviceConfig &MaemoSettingsWidget::currentConfig()
{
qDebug("%d/%d", m_ui->configListWidget->count(), m_devConfs.count());
Q_ASSERT(m_ui->configListWidget->count() == m_devConfs.count());
const QList<QListWidgetItem *> &selectedItems =
m_ui->configListWidget->selectedItems();
Q_ASSERT(selectedItems.count() == 1);
const int selectedRow = m_ui->configListWidget->row(selectedItems.first());
qDebug("selected row = %d", selectedRow);
Q_ASSERT(selectedRow < m_devConfs.count());
return m_devConfs[selectedRow];
}
void MaemoSettingsWidget::configNameEditingFinished()
{
const QString &newName = m_ui->nameLineEdit->text();
currentConfig().name = newName;
m_nameValidator.setName(newName);
m_ui->configListWidget->currentItem()->setText(newName);
}
void MaemoSettingsWidget::deviceTypeChanged()
{
currentConfig().type =
m_ui->deviceButton->isChecked()
? MaemoDeviceConfigurations::DeviceConfig::Physical
: MaemoDeviceConfigurations::DeviceConfig::Simulator;
}
void MaemoSettingsWidget::hostNameEditingFinished()
{
currentConfig().host = m_ui->hostLineEdit->text();
}
void MaemoSettingsWidget::portEditingFinished()
{
setPortOrTimeout(m_ui->portLineEdit, currentConfig().port, m_portValidator);
}
void MaemoSettingsWidget::timeoutEditingFinished()
{
setPortOrTimeout(m_ui->timeoutLineEdit, currentConfig().timeout,
m_timeoutValidator);
}
void MaemoSettingsWidget::setPortOrTimeout(const QLineEdit *lineEdit,
int &confVal, PortAndTimeoutValidator &validator)
{
bool ok;
confVal = lineEdit->text().toInt(&ok);
Q_ASSERT(ok);
validator.setValue(confVal);
}
void MaemoSettingsWidget::userNameEditingFinished()
{
currentConfig().uname = m_ui->userLineEdit->text();
}
void MaemoSettingsWidget::passwordEditingFinished()
{
currentConfig().pwd = m_ui->pwdLineEdit->text();
}
void MaemoSettingsWidget::selectionChanged()
{
const QList<QListWidgetItem *> &selectedItems =
m_ui->configListWidget->selectedItems();
Q_ASSERT(selectedItems.count() <= 1);
if (selectedItems.isEmpty()) {
m_ui->removeConfigButton->setEnabled(false);
clearDetails();
m_ui->detailsWidget->setEnabled(false);
} else {
m_ui->removeConfigButton->setEnabled(true);
display(currentConfig());
}
}
void MaemoSettingsWidget::clearDetails()
{
m_ui->nameLineEdit->clear();
m_ui->hostLineEdit->clear();
m_ui->portLineEdit->clear();
m_ui->timeoutLineEdit->clear();
m_ui->userLineEdit->clear();
m_ui->pwdLineEdit->clear();
}
} // namespace Internal } // namespace Internal
} // namespace Qt4ProjectManager } // namespace Qt4ProjectManager
......
...@@ -146,6 +146,9 @@ ...@@ -146,6 +146,9 @@
</item> </item>
<item> <item>
<widget class="QPushButton" name="removeConfigButton"> <widget class="QPushButton" name="removeConfigButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text"> <property name="text">
<string>Remove</string> <string>Remove</string>
</property> </property>
...@@ -169,5 +172,194 @@ ...@@ -169,5 +172,194 @@
</layout> </layout>
</widget> </widget>
<resources/> <resources/>
<connections/> <connections>
<connection>
<sender>nameLineEdit</sender>
<signal>editingFinished()</signal>
<receiver>maemoSettingsWidget</receiver>
<slot>configNameEditingFinished()</slot>
<hints>
<hint type="sourcelabel">
<x>221</x>
<y>166</y>
</hint>
<hint type="destinationlabel">
<x>514</x>
<y>321</y>
</hint>
</hints>
</connection>
<connection>
<sender>deviceButton</sender>
<signal>toggled(bool)</signal>
<receiver>maemoSettingsWidget</receiver>
<slot>deviceTypeChanged()</slot>
<hints>
<hint type="sourcelabel">
<x>164</x>
<y>197</y>
</hint>
<hint type="destinationlabel">
<x>511</x>
<y>279</y>
</hint>
</hints>
</connection>
<connection>
<sender>hostLineEdit</sender>
<signal>editingFinished()</signal>
<receiver>maemoSettingsWidget</receiver>
<slot>hostNameEditingFinished()</slot>
<hints>
<hint type="sourcelabel">
<x>289</x>
<y>222</y>
</hint>
<hint type="destinationlabel">
<x>424</x>
<y>231</y>
</hint>
</hints>
</connection>
<connection>
<sender>portLineEdit</sender>
<signal>editingFinished()</signal>
<receiver>maemoSettingsWidget</receiver>
<slot>portEditingFinished()</slot>
<hints>
<hint type="sourcelabel">
<x>320</x>
<y>248</y>
</hint>
<hint type="destinationlabel">
<x>514</x>
<y>352</y>
</hint>
</hints>
</connection>
<connection>
<sender>timeoutLineEdit</sender>
<signal>editingFinished()</signal>
<receiver>maemoSettingsWidget</receiver>
<slot>timeoutEditingFinished()</slot>
<hints>
<hint type="sourcelabel">
<x>371</x>
<y>276</y>
</hint>
<hint type="destinationlabel">
<x>425</x>
<y>187</y>
</hint>
</hints>
</connection>
<connection>
<sender>userLineEdit</sender>
<signal>editingFinished()</signal>
<receiver>maemoSettingsWidget</receiver>
<slot>userNameEditingFinished()</slot>
<hints>
<hint type="sourcelabel">
<x>340</x>
<y>302</y>
</hint>
<hint type="destinationlabel">
<x>422</x>
<y>301</y>
</hint>
</hints>
</connection>
<connection>
<sender>pwdLineEdit</sender>
<signal>editingFinished()</signal>
<receiver>maemoSettingsWidget</receiver>
<slot>passwordEditingFinished()</slot>
<hints>
<hint type="sourcelabel">
<x>243</x>
<y>333</y>
</hint>
<hint type="destinationlabel">
<x>423</x>
<y>336</y>
</hint>
</hints>
</connection>
<connection>
<sender>simulatorButton</sender>
<signal>toggled(bool)</signal>
<receiver>maemoSettingsWidget</receiver>
<slot>deviceTypeChanged()</slot>
<hints>
<hint type="sourcelabel">
<x>291</x>
<y>198</y>
</hint>
<hint type="destinationlabel">
<x>426</x>
<y>87</y>
</hint>
</hints>
</connection>
<connection>
<sender>addConfigButton</sender>
<signal>clicked()</signal>
<receiver>maemoSettingsWidget</receiver>
<slot>addConfig()</slot>
<hints>
<hint type="sourcelabel">
<x>465</x>
<y>27</y>
</hint>