Kstars

observeradd.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Prakash Mohan <prakash.mohan@kdemail.net>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "observeradd.h"
8
9#include "kstarsdata.h"
10
11#include <QSqlTableModel>
12
14{
15 // Setting up the widget from the .ui file and adding it to the QDialog
16 QWidget *widget = new QWidget;
17
18 ui.setupUi(widget);
19
20 QVBoxLayout *mainLayout = new QVBoxLayout;
21 mainLayout->addWidget(widget);
22 setLayout(mainLayout);
23
24 setWindowTitle(i18nc("@title:window", "Manage Observers"));
25
27 mainLayout->addWidget(buttonBox);
28 connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
29
30 ui.AddObserverB->setIcon(QIcon::fromTheme("list-add"));
31 ui.RemoveObserverB->setIcon(QIcon::fromTheme("list-remove"));
32
33 // Load the observers list from the file
35 auto db = QSqlDatabase::database(KStarsData::Instance()->userdb()->connectionName());
36 QSqlTableModel *users = new QSqlTableModel(nullptr, db);
37 users->setTable("user");
38 users->select();
39 ui.tableView->setModel(users);
40 ui.tableView->setColumnHidden(0, true);
41 ui.tableView->horizontalHeader()->resizeContentsPrecision();
42 ui.tableView->viewport()->update();
43 ui.AddObserverB->setEnabled(false);
44 ui.RemoveObserverB->setEnabled(false);
45 ui.tableView->setSelectionMode(QAbstractItemView::SingleSelection);
46 // Make connections
47 connect(ui.AddObserverB, SIGNAL(clicked()), this, SLOT(slotAddObserver()));
48 connect(ui.RemoveObserverB, SIGNAL(clicked()), this, SLOT(slotRemoveObserver()));
49 connect(ui.Name, SIGNAL(textChanged(QString)), this, SLOT(checkObserverInfo()));
50 connect(ui.Surname, SIGNAL(textChanged(QString)), this, SLOT(checkObserverInfo()));
51 //connect (ui.tableView->verticalHeader(),SIGNAL(sectionClicked(int)),this,SLOT(checkTableInfo()));
52 connect(ui.tableView, SIGNAL(clicked(QModelIndex)), this, SLOT(auxSlot()));
53}
55{
56 ui.RemoveObserverB->setEnabled(true);
57}
58
59void ObserverAdd::checkObserverInfo()
60{
61 if (ui.Name->text().isEmpty() || ui.Surname->text().isEmpty())
62 ui.AddObserverB->setEnabled(false);
63 else
64 ui.AddObserverB->setEnabled(true);
65}
66
67void ObserverAdd::slotUpdateModel()
68{
69 auto db = QSqlDatabase::database(KStarsData::Instance()->userdb()->connectionName());
70 QSqlTableModel *users = new QSqlTableModel(nullptr, db);
71 users->setTable("user");
72 users->select();
73 ui.tableView->setModel(users);
74 ui.tableView->setColumnHidden(0, true);
75 ui.tableView->horizontalHeader()->resizeContentsPrecision();
76 ui.tableView->viewport()->update();
77}
78
79void ObserverAdd::slotRemoveObserver()
80{
81 QModelIndex index = ui.tableView->currentIndex();
82 int nr = index.row();
83 QString s = ui.tableView->model()->data(ui.tableView->model()->index(nr, 0)).toString();
84
85 KStarsData::Instance()->userdb()->DeleteObserver(s);
86 ui.RemoveObserverB->setEnabled(false);
87 slotUpdateModel();
88}
89
90void ObserverAdd::slotAddObserver()
91{
92 if (KStarsData::Instance()->userdb()->FindObserver(ui.Name->text(), ui.Surname->text()))
93 {
94 if (OAL::warningOverwrite(
95 i18n("Another Observer already exists with the given Name and Surname, Overwrite?")) == KMessageBox::Cancel)
96 return;
97 }
98
99 KStarsData::Instance()->userdb()->AddObserver(ui.Name->text(), ui.Surname->text(), ui.Contact->text());
100
101 //Reload observers into OAL::m_observers
103 // Reset the UI for a fresh addition
104 ui.Name->clear();
105 ui.Surname->clear();
106 ui.Contact->clear();
107 slotUpdateModel();
108}
109
111{
112 KStarsData::Instance()->logObject()->readObservers();
113}
bool AddObserver(const QString &name, const QString &surname, const QString &contact)
Adds a new observer into the database.
Definition ksuserdb.cpp:624
bool DeleteObserver(const QString &id)
Removes the user with unique id as given by FindObserver Returns false if the user is not found.
Definition ksuserdb.cpp:686
KSUserDB * userdb()
Definition kstarsdata.h:217
OAL::Log * logObject()
Return log object.
Definition kstarsdata.h:343
void loadObservers()
function to load the list of observers from the file
ObserverAdd()
The default constructor.
void auxSlot()
function to add the new observer to the observerList of the global logObject
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
virtual void reject()
void rejected()
QIcon fromTheme(const QString &name)
int row() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QSqlDatabase database(const QString &connectionName, bool open)
virtual bool select()
virtual void setTable(const QString &tableName)
QWidget(QWidget *parent, Qt::WindowFlags f)
void setLayout(QLayout *layout)
void setWindowTitle(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.