Kstars

observeradd.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Prakash Mohan <[email protected]>
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
34  loadObservers();
35  QSqlDatabase db = KStarsData::Instance()->userdb()->GetDatabase();
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 
59 void 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 
67 void ObserverAdd::slotUpdateModel()
68 {
69  QSqlDatabase db = KStarsData::Instance()->userdb()->GetDatabase();
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 
79 void 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 
90 void 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::No)
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
102  loadObservers();
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 }
QIcon fromTheme(const QString &name)
KSUserDB * userdb()
Definition: kstarsdata.h:214
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:518
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void AddObserver(const QString &name, const QString &surname, const QString &contact)
Adds a new observer into the database.
Definition: ksuserdb.cpp:472
void auxSlot()
function to add the new observer to the observerList of the global logObject
Definition: observeradd.cpp:54
QString i18n(const char *text, const TYPE &arg...)
virtual void setTable(const QString &tableName)
void setupUi(QWidget *widget)
int row() const const
OAL::Log * logObject()
Return log object.
Definition: kstarsdata.h:340
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QChar * data()
virtual bool select()
void loadObservers()
function to load the list of observers from the file
ObserverAdd()
The default constructor.
Definition: observeradd.cpp:13
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Aug 8 2022 04:13:23 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.