1
0
forked from blue/squawk

Reformating, adding accounts ui dummy

This commit is contained in:
Blue 2019-03-30 23:13:13 +03:00
parent de36fe2a4e
commit 6823b41f24
16 changed files with 551 additions and 31 deletions

View File

@ -2,9 +2,11 @@
using namespace Core;
Account::Account(const QString& p_jid, const QString& p_password, QObject* parent):
Account::Account(const QString& p_login, const QString& p_server, const QString& p_password, const QString& p_name, QObject* parent):
QObject(parent),
jid(p_jid),
name(p_name),
login(p_login),
server(p_server),
password(p_password),
client()
{

View File

@ -11,11 +11,13 @@ namespace Core
class Account : public QObject
{
public:
Account(const QString& p_jid, const QString& p_password, QObject* parent = 0);
Account(const QString& p_login, const QString& p_server, const QString& p_password, const QString& p_name, QObject* parent = 0);
~Account();
private:
QString jid;
QString name;
QString login;
QString server;
QString password;
QXmppClient client;
};

View File

@ -1,15 +1,47 @@
#include "squawk.h"
#include <QDebug>
using namespace Core;
Squawk::Squawk(QObject* parent):
Core::Squawk::Squawk(QObject* parent):
QObject(parent),
accounts()
{
}
Squawk::~Squawk()
Core::Squawk::~Squawk()
{
Accounts::const_iterator itr = accounts.begin();
Accounts::const_iterator end = accounts.end();
for (; itr != end; ++itr) {
(*itr)->deleteLater();
}
}
void Core::Squawk::start()
{
}
void Core::Squawk::newAccountRequest(const QMap<QString, QVariant>& map)
{
QString name = map.value("name").toString();
QString login = map.value("login").toString();
QString server = map.value("server").toString();
QString password = map.value("password").toString();
addAccount(login, server, password, name);
}
void Core::Squawk::addAccount(const QString& login, const QString& server, const QString& password, const QString& name)
{
Account* acc = new Account(login, server, password, name);
accounts.push_back(acc);
QMap<QString, QVariant> map = {
{"login", login},
{"server", server},
{"name", name},
{"password", password},
{"state", 0}
};
emit newAccount(map);
}

View File

@ -2,6 +2,9 @@
#define CORE_SQUAWK_H
#include <QtCore/QObject>
#include <QString>
#include <QVariant>
#include <QMap>
#include <deque>
#include "account.h"
@ -16,10 +19,20 @@ public:
Squawk(QObject* parent = 0);
~Squawk();
signals:
void newAccount(const QMap<QString, QVariant>&);
public slots:
void start();
void newAccountRequest(const QMap<QString, QVariant>& map);
private:
typedef std::deque<Account> Accounts;
typedef std::deque<Account*> Accounts;
Accounts accounts;
private:
void addAccount(const QString& login, const QString& server, const QString& password, const QString& name);
};
}

View File

@ -15,6 +15,12 @@ int main(int argc, char *argv[])
squawk->moveToThread(coreThread);
QObject::connect(coreThread, SIGNAL(finished()), squawk, SLOT(deleteLater()));
QObject::connect(coreThread, SIGNAL(started()), squawk, SLOT(start()));
QObject::connect(&w, SIGNAL(newAccountRequest(const QMap<QString, QVariant>&)), squawk, SLOT(newAccountRequest(const QMap<QString, QVariant>&)));
QObject::connect(squawk, SIGNAL(newAccount(const QMap<QString, QVariant>&)), &w, SLOT(newAccount(const QMap<QString, QVariant>&)));
//QObject::connect(this, &Controller::operate, worker, &Worker::doWork);
//QObject::connect(worker, &Worker::resultReady, this, &Controller::handleResults);
coreThread->start();

View File

@ -12,6 +12,8 @@ find_package(Qt5Widgets CONFIG REQUIRED)
set(squawkUI_SRC
squawk.cpp
accounts.cpp
account.cpp
models/accounts.cpp
)
# Tell CMake to create the helloworld executable

23
ui/account.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "account.h"
#include "ui_account.h"
Account::Account()
: m_ui ( new Ui::Account )
{
m_ui->setupUi ( this );
}
Account::~Account()
{
}
QMap<QString, QVariant> Account::value() const
{
QMap<QString, QVariant> map;
map["login"] = m_ui->login->text();
map["password"] = m_ui->password->text();
map["server"] = m_ui->server->text();
map["name"] = m_ui->name->text();
return map;
}

29
ui/account.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <QScopedPointer>
#include <QDialog>
#include <QMap>
#include <QString>
#include <QVariant>
namespace Ui
{
class Account;
}
class Account : public QDialog
{
Q_OBJECT
public:
Account();
~Account();
QMap<QString, QVariant> value() const;
private:
QScopedPointer<Ui::Account> m_ui;
};
#endif // ACCOUNT_H

154
ui/account.ui Normal file
View File

@ -0,0 +1,154 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Account</class>
<widget class="QDialog" name="Account">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>395</width>
<height>272</height>
</rect>
</property>
<property name="windowTitle">
<string>Account</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout" stretch="0,0">
<property name="leftMargin">
<number>6</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>6</number>
</property>
<property name="bottomMargin">
<number>6</number>
</property>
<item>
<layout class="QFormLayout" name="formLayout">
<property name="horizontalSpacing">
<number>6</number>
</property>
<property name="verticalSpacing">
<number>6</number>
</property>
<item row="1" column="1">
<widget class="QLineEdit" name="login">
<property name="toolTip">
<string>Your account login</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Server</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="server">
<property name="toolTip">
<string>A server address of your account. Like 404.city or macaw.me</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Login</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Password</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="password">
<property name="toolTip">
<string>Password of your account</string>
</property>
<property name="inputMask">
<string/>
</property>
<property name="text">
<string/>
</property>
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
<property name="clearButtonEnabled">
<bool>false</bool>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Name</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="name">
<property name="toolTip">
<string>Just a name how would you call this account, doesn't affect anything</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>Account</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>20</x>
<y>20</y>
</hint>
<hint type="destinationlabel">
<x>20</x>
<y>20</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>Account</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>20</x>
<y>20</y>
</hint>
<hint type="destinationlabel">
<x>20</x>
<y>20</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -1,10 +1,43 @@
#include "accounts.h"
#include "ui_accounts.h"
#include <QDebug>
Accounts::Accounts(QWidget *parent) :
m_ui(new Ui::Accounts)
m_ui(new Ui::Accounts),
tableModel()
{
m_ui->setupUi(this);
connect(m_ui->addButton, SIGNAL(clicked(bool)), this, SLOT(onAddButton(bool)));
m_ui->tableView->setModel(&tableModel);
}
Accounts::~Accounts() = default;
void Accounts::onAddButton(bool clicked)
{
Account* acc = new Account();
connect(acc, SIGNAL(accepted()), this, SLOT(onAccountAccepted()));
connect(acc, SIGNAL(rejected()), this, SLOT(onAccountRejected()));
acc->exec();
}
void Accounts::onAccountAccepted()
{
Account* acc = static_cast<Account*>(sender());
QMap<QString, QVariant> map = acc->value();
emit newAccount(map);
}
void Accounts::onAccountRejected()
{
Account* acc = static_cast<Account*>(sender());
acc->deleteLater();
}
void Accounts::addAccount(const QMap<QString, QVariant>& map)
{
tableModel.addAccount(map);
}

View File

@ -4,14 +4,14 @@
#include <QWidget>
#include <QScopedPointer>
#include "account.h"
#include "models/accounts.h"
namespace Ui
{
class Accounts;
}
/**
* @todo write docs
*/
class Accounts : public QWidget
{
Q_OBJECT
@ -19,8 +19,19 @@ public:
explicit Accounts(QWidget *parent = nullptr);
~Accounts() override;
void addAccount(const QMap<QString, QVariant>&);
signals:
void newAccount(const QMap<QString, QVariant>&);
private slots:
void onAddButton(bool clicked = 0);
void onAccountAccepted();
void onAccountRejected();
private:
QScopedPointer<Ui::Accounts> m_ui;
Models::Accounts tableModel;
};
#endif // ACCOUNTS_H

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>768</width>
<height>235</height>
<width>539</width>
<height>249</height>
</rect>
</property>
<property name="windowTitle">
@ -15,20 +15,71 @@
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>0</number>
<number>6</number>
</property>
<property name="topMargin">
<number>0</number>
<number>6</number>
</property>
<property name="rightMargin">
<number>0</number>
<number>6</number>
</property>
<property name="bottomMargin">
<number>0</number>
<number>6</number>
</property>
<property name="spacing">
<number>0</number>
<number>6</number>
</property>
<item row="4" column="1">
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QPushButton" name="deleteButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Delete</string>
</property>
</widget>
</item>
<item row="0" column="0" rowspan="8">
<widget class="QTableView" name="tableView">
<property name="minimumSize">
<size>
<width>400</width>
<height>0</height>
</size>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
<property name="showGrid">
<bool>false</bool>
</property>
<property name="gridStyle">
<enum>Qt::SolidLine</enum>
</property>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<attribute name="verticalHeaderHighlightSections">
<bool>false</bool>
</attribute>
</widget>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="addButton">
<property name="text">
@ -38,12 +89,25 @@
</item>
<item row="1" column="1">
<widget class="QPushButton" name="editButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Edit</string>
</property>
</widget>
</item>
<item row="3" column="1">
<item row="5" column="1">
<widget class="QPushButton" name="changePassword">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Change password</string>
</property>
</widget>
</item>
<item row="7" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
@ -56,16 +120,16 @@
</property>
</spacer>
</item>
<item row="2" column="1">
<widget class="QPushButton" name="deleteButton">
<item row="6" column="1">
<widget class="QPushButton" name="connect">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Delete</string>
<string>Connect</string>
</property>
</widget>
</item>
<item row="0" column="0" rowspan="4">
<widget class="QTableView" name="tableView"/>
</item>
</layout>
</widget>
<resources/>

77
ui/models/accounts.cpp Normal file
View File

@ -0,0 +1,77 @@
#include "accounts.h"
std::deque<QString> Models::Accounts::columns = {
"name",
"server",
"state"
};
Models::Accounts::Accounts(QObject* parent):
QAbstractTableModel(parent),
accs()
{
}
Models::Accounts::~Accounts()
{
}
QVariant Models::Accounts::data ( const QModelIndex& index, int role ) const
{
QVariant answer;
switch (role) {
case Qt::DisplayRole: {
const Account& acc = accs[index.row()];
switch (index.column()) {
case 0:
answer = acc.name;
break;
case 1:
answer = acc.server;
break;
case 2:
answer = acc.state;
break;
}
}
break;
default:
break;
}
return answer;
}
int Models::Accounts::columnCount ( const QModelIndex& parent ) const
{
return columns.size();
}
int Models::Accounts::rowCount ( const QModelIndex& parent ) const
{
return accs.size();
}
QVariant Models::Accounts::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
return columns[section];
}
return QVariant();
}
void Models::Accounts::addAccount(const QMap<QString, QVariant>& map)
{
beginInsertRows(QModelIndex(), accs.size(), accs.size());
accs.push_back({
map.value("name").toString(),
map.value("server").toString(),
map.value("login").toString(),
map.value("password").toString(),
map.value("state").toInt()
});
endInsertRows();
}

43
ui/models/accounts.h Normal file
View File

@ -0,0 +1,43 @@
#ifndef MODELS_ACCOUNTS_H
#define MODELS_ACCOUNTS_H
#include <qabstractitemmodel.h>
#include <deque>
namespace Models
{
struct Account;
class Accounts : public QAbstractTableModel
{
Q_OBJECT
public:
Accounts(QObject* parent = 0);
~Accounts();
void addAccount(const QMap<QString, QVariant>& map);
QVariant data ( const QModelIndex& index, int role ) const override;
int columnCount ( const QModelIndex& parent ) const override;
int rowCount ( const QModelIndex& parent ) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
private:
std::deque<Account> accs;
static std::deque<QString> columns;
};
struct Account {
QString name;
QString server;
QString login;
QString password;
int state;
};
}
#endif // MODELS_ACCOUNT_H

View File

@ -4,7 +4,8 @@
Squawk::Squawk(QWidget *parent) :
QMainWindow(parent),
m_ui(new Ui::Squawk),
accounts(0)
accounts(0),
accountsCache()
{
m_ui->setupUi(this);
@ -21,9 +22,20 @@ void Squawk::onAccounts()
accounts = new Accounts(this);
accounts->setAttribute(Qt::WA_DeleteOnClose);
connect(accounts, SIGNAL(destroyed(QObject*)), this, SLOT(onAccountsClosed(QObject*)));
connect(accounts, SIGNAL(newAccount(const QMap<QString, QVariant>&)), this, SIGNAL(newAccountRequest(const QMap<QString, QVariant>&)));
AC::const_iterator itr = accountsCache.begin();
AC::const_iterator end = accountsCache.end();
for (; itr != end; ++itr) {
accounts->addAccount(*itr);
}
accounts->show();
} else {
accounts->focusWidget();
accounts->show();
accounts->raise();
accounts->activateWindow();
}
}
@ -41,3 +53,11 @@ void Squawk::onAccountsClosed(QObject* parent)
{
accounts = 0;
}
void Squawk::newAccount(const QMap<QString, QVariant>& account)
{
accountsCache.push_back(account);
if (accounts != 0) {
accounts->addAccount(account);
}
}

View File

@ -4,6 +4,7 @@
#include <QMainWindow>
#include <QScopedPointer>
#include <QCloseEvent>
#include <deque>
#include "accounts.h"
@ -18,11 +19,19 @@ class Squawk : public QMainWindow
public:
explicit Squawk(QWidget *parent = nullptr);
~Squawk() override;
signals:
void newAccountRequest(const QMap<QString, QVariant>&);
public slots:
void newAccount(const QMap<QString, QVariant>& account);
private:
typedef std::deque<QMap<QString, QVariant>> AC;
QScopedPointer<Ui::Squawk> m_ui;
Accounts* accounts;
AC accountsCache;
protected:
void closeEvent(QCloseEvent * event) override;