setting works
diff --git a/chatdialog.cpp b/chatdialog.cpp
index ca1b3ae..34f5682 100644
--- a/chatdialog.cpp
+++ b/chatdialog.cpp
@@ -3,23 +3,17 @@
#include "settingdialog.h"
#include <ctime>
#include <iostream>
+#include <QTimer>
ChatDialog::ChatDialog(QWidget *parent)
: QDialog(parent)
{
setupUi(this);
+
+ readSettings();
+ updateLabels();
+
lineEdit->setFocusPolicy(Qt::StrongFocus);
-
- // for test only
- m_nick = "Tester";
- m_chatroom = "Test";
- m_prefix = "/ndn/ucla.edu/cs/tester";
-
- QString settingDisp = QString("<User: %1>, <Chatroom: %2>").arg(m_nick).arg(m_chatroom);
- infoLabel->setText(settingDisp);
- QString prefixDisp = QString("<Prefix: %1>").arg(m_prefix);
- prefixLabel->setText(prefixDisp);
-
DigestTreeScene *scene = new DigestTreeScene();
treeViewer->setScene(scene);
@@ -66,6 +60,36 @@
msg.set_timestamp(seconds);
}
+void
+ChatDialog::readSettings()
+{
+ QSettings s(ORGANIZATION, APPLICATION);
+ m_nick = s.value("nick", "").toString();
+ m_chatroom = s.value("chatroom", "").toString();
+ m_prefix = s.value("prefix", "").toString();
+ if (m_nick == "" || m_chatroom == "" || m_prefix == "") {
+ QTimer::singleShot(500, this, SLOT(buttonPressed()));
+ }
+}
+
+void
+ChatDialog::writeSettings()
+{
+ QSettings s(ORGANIZATION, APPLICATION);
+ s.setValue("nick", m_nick);
+ s.setValue("chatroom", m_chatroom);
+ s.setValue("prefix", m_prefix);
+}
+
+void
+ChatDialog::updateLabels()
+{
+ QString settingDisp = QString("<User: %1>, <Chatroom: %2>").arg(m_nick).arg(m_chatroom);
+ infoLabel->setText(settingDisp);
+ QString prefixDisp = QString("<Prefix: %1>").arg(m_prefix);
+ prefixLabel->setText(prefixDisp);
+}
+
void
ChatDialog::returnPressed()
{
@@ -88,7 +112,30 @@
ChatDialog::buttonPressed()
{
SettingDialog dialog(this, m_nick, m_chatroom, m_prefix);
+ connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString)));
dialog.exec();
- setButton->setFocusPolicy(Qt::NoFocus);
}
+void
+ChatDialog::settingUpdated(QString nick, QString chatroom, QString prefix)
+{
+ bool needWrite = false;
+ if (!nick.isEmpty() && nick != m_nick) {
+ m_nick = nick;
+ needWrite = true;
+ }
+ if (!prefix.isEmpty() && prefix != m_prefix) {
+ m_prefix = prefix;
+ needWrite = true;
+ // TODO: set the previous prefix as left?
+ }
+ if (!chatroom.isEmpty() && chatroom != m_chatroom) {
+ m_chatroom = chatroom;
+ needWrite = true;
+ // TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet
+ }
+ if (needWrite) {
+ writeSettings();
+ updateLabels();
+ }
+}
diff --git a/chatdialog.h b/chatdialog.h
index e30c05e..337f20f 100644
--- a/chatdialog.h
+++ b/chatdialog.h
@@ -9,6 +9,9 @@
#include <sync-logic.h>
#include <sync-seq-no.h>
+#define ORGANIZATION "IRL@UCLA"
+#define APPLICATION "SYNC-DEMO"
+
class ChatDialog : public QDialog, private Ui::ChatDialog
{
Q_OBJECT
@@ -20,10 +23,14 @@
private:
void formChatMessage(const QString &text, SyncDemo::ChatMessage &msg);
+ void readSettings();
+ void writeSettings();
+ void updateLabels();
private slots:
void returnPressed();
void buttonPressed();
+ void settingUpdated(QString, QString, QString);
private:
QString m_nick;
diff --git a/main.cpp b/main.cpp
index 8f24743..6b73923 100644
--- a/main.cpp
+++ b/main.cpp
@@ -7,5 +7,6 @@
ChatDialog dialog;
dialog.show();
+ dialog.activateWindow();
return app.exec();
}
diff --git a/settingdialog.cpp b/settingdialog.cpp
index 8719a84..be0bb42 100644
--- a/settingdialog.cpp
+++ b/settingdialog.cpp
@@ -8,5 +8,12 @@
roomEdit->setPlaceholderText(chatroom);
prefixEdit->setPlaceholderText(prefix);
connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
- connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
+ connect(okButton, SIGNAL(clicked()), this, SLOT(update()));
+}
+
+void
+SettingDialog::update()
+{
+ emit updated(nickEdit->text(), roomEdit->text(), prefixEdit->text());
+ accept();
}
diff --git a/settingdialog.h b/settingdialog.h
index 5487cde..ac63777 100644
--- a/settingdialog.h
+++ b/settingdialog.h
@@ -8,6 +8,13 @@
public:
SettingDialog(QWidget *parent = 0, QString nick = QString("NULL"), QString chatroom = QString("NULL"), QString prefix = QString("NULL"));
+
+private slots:
+ void update();
+
+signals:
+ void updated(QString, QString, QString);
+
};
#endif
diff --git a/settingdialog.ui b/settingdialog.ui
index dff4408..b848aa9 100644
--- a/settingdialog.ui
+++ b/settingdialog.ui
@@ -59,13 +59,25 @@
<number>6</number>
</property>
<item>
- <widget class="QLineEdit" name="nickEdit" />
+ <widget class="QLineEdit" name="nickEdit">
+ <property name="maxLength">
+ <number>200</number>
+ </property>
+ </widget>
</item>
<item>
- <widget class="QLineEdit" name="roomEdit" />
+ <widget class="QLineEdit" name="roomEdit">
+ <property name="maxLength">
+ <number>200</number>
+ </property>
+ </widget>
</item>
<item>
- <widget class="QLineEdit" name="prefixEdit" />
+ <widget class="QLineEdit" name="prefixEdit">
+ <property name="maxLength">
+ <number>200</number>
+ </property>
+ </widget>
</item>
</layout>
</item>