Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 1 | /* -*- 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 Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 11 | #include "profile-editor.hpp" |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 12 | #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 Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 21 | // INIT_LOGGER("ProfileEditor") |
| 22 | |
| 23 | namespace chronos { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 24 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 25 | ProfileEditor::ProfileEditor(QWidget *parent) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 26 | : 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 | |
| 40 | ProfileEditor::~ProfileEditor() |
| 41 | { |
| 42 | delete ui; |
| 43 | delete m_tableModel; |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | ProfileEditor::onCloseDBModule() |
| 48 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 49 | // _LOG_DEBUG("close db module"); |
| 50 | if (m_tableModel) { |
| 51 | delete m_tableModel; |
| 52 | // _LOG_DEBUG("tableModel closed"); |
| 53 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void |
| 57 | ProfileEditor::onIdentityUpdated(const QString& identity) |
| 58 | { |
| 59 | m_tableModel = new QSqlTableModel(); |
| 60 | |
| 61 | m_identity = identity; |
| 62 | ui->identityInput->setText(identity); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 63 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 64 | 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 | |
| 74 | void |
| 75 | ProfileEditor::onAddClicked() |
| 76 | { |
| 77 | int rowCount = m_tableModel->rowCount(); |
| 78 | QSqlRecord record; |
| 79 | m_tableModel->insertRow(rowCount); |
| 80 | m_tableModel->setRecord(rowCount, record); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | ProfileEditor::onDeleteClicked() |
| 85 | { |
| 86 | QItemSelectionModel* selectionModel = ui->profileTable->selectionModel(); |
| 87 | QModelIndexList indexList = selectionModel->selectedIndexes(); |
| 88 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 89 | for (int i = indexList.size() - 1; i >= 0; i--) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 90 | m_tableModel->removeRow(indexList[i].row()); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 91 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 92 | m_tableModel->submitAll(); |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | ProfileEditor::onOkClicked() |
| 97 | { |
| 98 | m_tableModel->submitAll(); |
| 99 | emit updateProfile(); |
| 100 | this->hide(); |
| 101 | } |
| 102 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 103 | } // namespace chronos |
| 104 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 105 | #if WAF |
| 106 | #include "profile-editor.moc" |
Yingdi Yu | 4212586 | 2014-08-07 17:04:28 -0700 | [diff] [blame] | 107 | // #include "profile-editor.cpp.moc" |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 108 | #endif |