Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 3 | * Copyright (c) 2020, Regents of the University of California |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 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 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 17 | namespace chronochat { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 18 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 19 | ProfileEditor::ProfileEditor(QWidget *parent) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 20 | : QDialog(parent) |
| 21 | , ui(new Ui::ProfileEditor) |
| 22 | , m_tableModel(new QSqlTableModel()) |
| 23 | { |
| 24 | ui->setupUi(this); |
| 25 | |
| 26 | connect(ui->addRowButton, SIGNAL(clicked()), |
| 27 | this, SLOT(onAddClicked())); |
| 28 | connect(ui->deleteRowButton, SIGNAL(clicked()), |
| 29 | this, SLOT(onDeleteClicked())); |
| 30 | connect(ui->okButton, SIGNAL(clicked()), |
| 31 | this, SLOT(onOkClicked())); |
| 32 | } |
| 33 | |
| 34 | ProfileEditor::~ProfileEditor() |
| 35 | { |
| 36 | delete ui; |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 37 | delete m_tableModel; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void |
| 41 | ProfileEditor::onCloseDBModule() |
| 42 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 43 | if (m_tableModel) { |
| 44 | delete m_tableModel; |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 45 | m_tableModel = 0; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 46 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void |
| 50 | ProfileEditor::onIdentityUpdated(const QString& identity) |
| 51 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 52 | m_identity = identity; |
| 53 | ui->identityInput->setText(identity); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void |
| 57 | ProfileEditor::resetPanel() |
| 58 | { |
| 59 | m_tableModel = new QSqlTableModel(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 60 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 61 | m_tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 62 | m_tableModel->setTable("SelfProfile"); |
| 63 | m_tableModel->select(); |
| 64 | m_tableModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Type")); |
| 65 | m_tableModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Value")); |
| 66 | |
| 67 | ui->profileTable->setModel(m_tableModel); |
| 68 | ui->profileTable->show(); |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | ProfileEditor::onAddClicked() |
| 73 | { |
| 74 | int rowCount = m_tableModel->rowCount(); |
| 75 | QSqlRecord record; |
| 76 | m_tableModel->insertRow(rowCount); |
| 77 | m_tableModel->setRecord(rowCount, record); |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | ProfileEditor::onDeleteClicked() |
| 82 | { |
| 83 | QItemSelectionModel* selectionModel = ui->profileTable->selectionModel(); |
| 84 | QModelIndexList indexList = selectionModel->selectedIndexes(); |
| 85 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 86 | for (int i = indexList.size() - 1; i >= 0; i--) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 87 | m_tableModel->removeRow(indexList[i].row()); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 88 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 89 | m_tableModel->submitAll(); |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | ProfileEditor::onOkClicked() |
| 94 | { |
| 95 | m_tableModel->submitAll(); |
| 96 | emit updateProfile(); |
| 97 | this->hide(); |
| 98 | } |
| 99 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 100 | } // namespace chronochat |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 101 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 102 | #if WAF |
| 103 | #include "profile-editor.moc" |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 104 | #endif |