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 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 23 | namespace chronochat { |
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; |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 43 | |
| 44 | if (m_tableModel) |
| 45 | delete m_tableModel; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void |
| 49 | ProfileEditor::onCloseDBModule() |
| 50 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 51 | if (m_tableModel) { |
| 52 | delete m_tableModel; |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 53 | m_tableModel = 0; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 54 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void |
| 58 | ProfileEditor::onIdentityUpdated(const QString& identity) |
| 59 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 60 | m_identity = identity; |
| 61 | ui->identityInput->setText(identity); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 62 | } |
| 63 | |
| 64 | void |
| 65 | ProfileEditor::resetPanel() |
| 66 | { |
| 67 | m_tableModel = new QSqlTableModel(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 68 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 69 | m_tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 70 | m_tableModel->setTable("SelfProfile"); |
| 71 | m_tableModel->select(); |
| 72 | m_tableModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Type")); |
| 73 | m_tableModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Value")); |
| 74 | |
| 75 | ui->profileTable->setModel(m_tableModel); |
| 76 | ui->profileTable->show(); |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | ProfileEditor::onAddClicked() |
| 81 | { |
| 82 | int rowCount = m_tableModel->rowCount(); |
| 83 | QSqlRecord record; |
| 84 | m_tableModel->insertRow(rowCount); |
| 85 | m_tableModel->setRecord(rowCount, record); |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | ProfileEditor::onDeleteClicked() |
| 90 | { |
| 91 | QItemSelectionModel* selectionModel = ui->profileTable->selectionModel(); |
| 92 | QModelIndexList indexList = selectionModel->selectedIndexes(); |
| 93 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 94 | for (int i = indexList.size() - 1; i >= 0; i--) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 95 | m_tableModel->removeRow(indexList[i].row()); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 96 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 97 | m_tableModel->submitAll(); |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | ProfileEditor::onOkClicked() |
| 102 | { |
| 103 | m_tableModel->submitAll(); |
| 104 | emit updateProfile(); |
| 105 | this->hide(); |
| 106 | } |
| 107 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 108 | } // namespace chronochat |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 109 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 110 | #if WAF |
| 111 | #include "profile-editor.moc" |
Yingdi Yu | 4212586 | 2014-08-07 17:04:28 -0700 | [diff] [blame] | 112 | // #include "profile-editor.cpp.moc" |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 113 | #endif |