initial class reference

This commit is contained in:
Blue 2020-04-16 23:23:02 +03:00
parent a8698cc94f
commit 494afcf2b5
6 changed files with 161 additions and 6 deletions

View File

@ -24,6 +24,7 @@ set(squawkUI_SRC
models/room.cpp models/room.cpp
models/abstractparticipant.cpp models/abstractparticipant.cpp
models/participant.cpp models/participant.cpp
models/reference.cpp
utils/messageline.cpp utils/messageline.cpp
utils//message.cpp utils//message.cpp
utils/resizer.cpp utils/resizer.cpp

View File

@ -26,7 +26,8 @@ Models::Item::Item(Type p_type, const QMap<QString, QVariant> &p_data, Item *p_p
type(p_type), type(p_type),
name(""), name(""),
childItems(), childItems(),
parent(p_parent) parent(p_parent),
references()
{ {
QMap<QString, QVariant>::const_iterator itr = p_data.find("name"); QMap<QString, QVariant>::const_iterator itr = p_data.find("name");
if (itr != p_data.end()) { if (itr != p_data.end()) {

View File

@ -26,10 +26,12 @@
#include <deque> #include <deque>
#include "shared/enums.h" #include "shared/enums.h"
namespace Models { namespace Models {
class Reference;
class Item : public QObject{ class Item : public QObject{
friend class Reference;
Q_OBJECT Q_OBJECT
public: public:
enum Type { enum Type {
@ -39,7 +41,8 @@ class Item : public QObject{
room, room,
presence, presence,
participant, participant,
root root,
reference
}; };
explicit Item(Type p_type, const QMap<QString, QVariant> &data, Item *parentItem = 0); explicit Item(Type p_type, const QMap<QString, QVariant> &data, Item *parentItem = 0);
@ -62,8 +65,8 @@ class Item : public QObject{
QString getName() const; QString getName() const;
void setName(const QString& name); void setName(const QString& name);
Item *child(int row); virtual Item *child(int row);
int childCount() const; virtual int childCount() const;
virtual int columnCount() const; virtual int columnCount() const;
virtual QVariant data(int column) const; virtual QVariant data(int column) const;
int row() const; int row() const;
@ -92,6 +95,7 @@ class Item : public QObject{
QString name; QString name;
std::deque<Item*> childItems; std::deque<Item*> childItems;
Item* parent; Item* parent;
std::deque<Item*> references;
protected slots: protected slots:
virtual void toOfflineState(); virtual void toOfflineState();

93
ui/models/reference.cpp Normal file
View File

@ -0,0 +1,93 @@
/*
* Squawk messenger.
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "reference.h"
using namespace Models;
Models::Reference::Reference(Models::Item* original, Models::Item* parent):
Models::Item(reference, {}, parent),
original(original)
{
original->references.push_back(this);
connect(original, &Item::childChanged, this, &Item::childChanged);
connect(original, &Item::childIsAboutToBeInserted, this, &Item::childIsAboutToBeInserted);
connect(original, &Item::childInserted, this, &Item::childInserted);
connect(original, &Item::childIsAboutToBeRemoved, this, &Item::childIsAboutToBeRemoved);
connect(original, &Item::childRemoved, this, &Item::childRemoved);
connect(original, &Item::childIsAboutToBeMoved, this, &Item::childIsAboutToBeMoved);
connect(original, &Item::childMoved, this, &Item::childMoved);
}
Models::Reference::~Reference()
{
disconnect(original, &Item::childIsAboutToBeInserted, this, &Item::childIsAboutToBeInserted);
disconnect(original, &Item::childInserted, this, &Item::childInserted);
disconnect(original, &Item::childIsAboutToBeRemoved, this, &Item::childIsAboutToBeRemoved);
disconnect(original, &Item::childRemoved, this, &Item::childRemoved);
disconnect(original, &Item::childIsAboutToBeMoved, this, &Item::childIsAboutToBeMoved);
disconnect(original, &Item::childMoved, this, &Item::childMoved);
for (std::deque<Item*>::const_iterator itr = original->references.begin(), end = original->references.end(); itr != end; itr++) {
if (*itr == this) {
original->references.erase(itr);
break;
}
}
}
int Models::Reference::columnCount() const
{
return original->columnCount();
}
QVariant Models::Reference::data(int column) const
{
return original->data(column);
}
QString Models::Reference::getDisplayedName() const
{
return original->getDisplayedName();
}
Models::Item * Models::Reference::dereference()
{
return original;
}
const Models::Item * Models::Reference::dereferenceConst() const
{
return original;
}
void Models::Reference::appendChild(Models::Item* child)
{
original->appendChild(child);
}
void Models::Reference::removeChild(int index)
{
original->removeChild(index);
}
void Models::Reference::toOfflineState()
{
original->toOfflineState();
}

54
ui/models/reference.h Normal file
View File

@ -0,0 +1,54 @@
/*
* Squawk messenger.
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MODELS_REFERENCE_H
#define MODELS_REFERENCE_H
#include "item.h"
namespace Models {
/**
* @todo write docs
*/
class Reference : public Models::Item
{
Q_OBJECT
public:
Reference(Models::Item* original, Models::Item* parent);
~Reference();
int columnCount() const override;
QVariant data(int column) const override;
QString getDisplayedName() const override;
void appendChild(Models::Item * child) override;
void removeChild(int index) override;
Item* dereference();
const Item* dereferenceConst() const;
protected slots:
void toOfflineState() override;
private:
Models::Item* original;
};
}
#endif // MODELS_REFERENCE_H

View File

@ -480,7 +480,9 @@ void Models::Roster::addContact(const QString& account, const QString& jid, cons
if (item->type == Item::contact) { if (item->type == Item::contact) {
Contact* ca = static_cast<Contact*>(item); Contact* ca = static_cast<Contact*>(item);
if (ca->getJid() == jid) { if (ca->getJid() == jid) {
qDebug() << "An attempt to add a already existing contact " << jid << " to the group " << group << ", contact will be moved from ungrouped contacts of " << account; qDebug() << "An attempt to add a already existing contact " << jid
<< " to the group " << group
<< ", contact will be moved from ungrouped contacts of " << account;
parent->appendChild(ca); parent->appendChild(ca);
return; return;