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 | |
| 11 | #include "profile-editor.h" |
| 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 | |
| 21 | INIT_LOGGER("ProfileEditor") |
| 22 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 23 | ProfileEditor::ProfileEditor(QWidget *parent) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 24 | : QDialog(parent) |
| 25 | , ui(new Ui::ProfileEditor) |
| 26 | , m_tableModel(new QSqlTableModel()) |
| 27 | { |
| 28 | ui->setupUi(this); |
| 29 | |
| 30 | connect(ui->addRowButton, SIGNAL(clicked()), |
| 31 | this, SLOT(onAddClicked())); |
| 32 | connect(ui->deleteRowButton, SIGNAL(clicked()), |
| 33 | this, SLOT(onDeleteClicked())); |
| 34 | connect(ui->okButton, SIGNAL(clicked()), |
| 35 | this, SLOT(onOkClicked())); |
| 36 | } |
| 37 | |
| 38 | ProfileEditor::~ProfileEditor() |
| 39 | { |
| 40 | delete ui; |
| 41 | delete m_tableModel; |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | ProfileEditor::onCloseDBModule() |
| 46 | { |
| 47 | _LOG_DEBUG("close db module"); |
| 48 | if(m_tableModel) |
| 49 | { |
| 50 | delete m_tableModel; |
| 51 | _LOG_DEBUG("tableModel closed"); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | ProfileEditor::onIdentityUpdated(const QString& identity) |
| 57 | { |
| 58 | m_tableModel = new QSqlTableModel(); |
| 59 | |
| 60 | m_identity = identity; |
| 61 | ui->identityInput->setText(identity); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 62 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 63 | m_tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 64 | m_tableModel->setTable("SelfProfile"); |
| 65 | m_tableModel->select(); |
| 66 | m_tableModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Type")); |
| 67 | m_tableModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Value")); |
| 68 | |
| 69 | ui->profileTable->setModel(m_tableModel); |
| 70 | ui->profileTable->show(); |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | ProfileEditor::onAddClicked() |
| 75 | { |
| 76 | int rowCount = m_tableModel->rowCount(); |
| 77 | QSqlRecord record; |
| 78 | m_tableModel->insertRow(rowCount); |
| 79 | m_tableModel->setRecord(rowCount, record); |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | ProfileEditor::onDeleteClicked() |
| 84 | { |
| 85 | QItemSelectionModel* selectionModel = ui->profileTable->selectionModel(); |
| 86 | QModelIndexList indexList = selectionModel->selectedIndexes(); |
| 87 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 88 | int i = indexList.size() - 1; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 89 | for(; i >= 0; i--) |
| 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 | |
| 103 | #if WAF |
| 104 | #include "profile-editor.moc" |
| 105 | #include "profile-editor.cpp.moc" |
| 106 | #endif |