blob: 4e91a563925bce521422bbbba9a7903b59e8f4ca [file] [log] [blame]
Yingdi Yu348f5ea2014-03-01 14:47:25 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Yingdi Yu
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
9 */
10
Yingdi Yu0b0a7362014-08-05 16:31:30 -070011#include "profile-editor.hpp"
Yingdi Yu348f5ea2014-03-01 14:47:25 -080012#include "ui_profile-editor.h"
13#include <QtSql/QSqlRecord>
14#include <QtSql/QSqlField>
15#include <QtSql/QSqlError>
16
17#ifndef Q_MOC_RUN
18#include "logging.h"
19#endif
20
Yingdi Yu0b0a7362014-08-05 16:31:30 -070021// INIT_LOGGER("ProfileEditor")
22
23namespace chronos {
Yingdi Yu348f5ea2014-03-01 14:47:25 -080024
Yingdi Yufa0b6a02014-04-30 14:26:42 -070025ProfileEditor::ProfileEditor(QWidget *parent)
Yingdi Yu348f5ea2014-03-01 14:47:25 -080026 : QDialog(parent)
27 , ui(new Ui::ProfileEditor)
28 , m_tableModel(new QSqlTableModel())
29{
30 ui->setupUi(this);
31
32 connect(ui->addRowButton, SIGNAL(clicked()),
33 this, SLOT(onAddClicked()));
34 connect(ui->deleteRowButton, SIGNAL(clicked()),
35 this, SLOT(onDeleteClicked()));
36 connect(ui->okButton, SIGNAL(clicked()),
37 this, SLOT(onOkClicked()));
38}
39
40ProfileEditor::~ProfileEditor()
41{
42 delete ui;
43 delete m_tableModel;
44}
45
46void
47ProfileEditor::onCloseDBModule()
48{
Yingdi Yu0b0a7362014-08-05 16:31:30 -070049 // _LOG_DEBUG("close db module");
50 if (m_tableModel) {
51 delete m_tableModel;
52 // _LOG_DEBUG("tableModel closed");
53 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -080054}
55
56void
57ProfileEditor::onIdentityUpdated(const QString& identity)
58{
59 m_tableModel = new QSqlTableModel();
60
61 m_identity = identity;
62 ui->identityInput->setText(identity);
Yingdi Yufa0b6a02014-04-30 14:26:42 -070063
Yingdi Yu348f5ea2014-03-01 14:47:25 -080064 m_tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
65 m_tableModel->setTable("SelfProfile");
66 m_tableModel->select();
67 m_tableModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Type"));
68 m_tableModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Value"));
69
70 ui->profileTable->setModel(m_tableModel);
71 ui->profileTable->show();
72}
73
74void
75ProfileEditor::onAddClicked()
76{
77 int rowCount = m_tableModel->rowCount();
78 QSqlRecord record;
79 m_tableModel->insertRow(rowCount);
80 m_tableModel->setRecord(rowCount, record);
81}
82
83void
84ProfileEditor::onDeleteClicked()
85{
86 QItemSelectionModel* selectionModel = ui->profileTable->selectionModel();
87 QModelIndexList indexList = selectionModel->selectedIndexes();
88
Yingdi Yu0b0a7362014-08-05 16:31:30 -070089 for (int i = indexList.size() - 1; i >= 0; i--)
Yingdi Yu348f5ea2014-03-01 14:47:25 -080090 m_tableModel->removeRow(indexList[i].row());
Yingdi Yufa0b6a02014-04-30 14:26:42 -070091
Yingdi Yu348f5ea2014-03-01 14:47:25 -080092 m_tableModel->submitAll();
93}
94
95void
96ProfileEditor::onOkClicked()
97{
98 m_tableModel->submitAll();
99 emit updateProfile();
100 this->hide();
101}
102
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700103} // namespace chronos
104
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800105#if WAF
106#include "profile-editor.moc"
107#include "profile-editor.cpp.moc"
108#endif