|
| 1 | +#include "Config.h" |
| 2 | +#include <QDebug> |
| 3 | +Config::Config(QWidget *parent) |
| 4 | + : QWidget(parent) { |
| 5 | + ui.setupUi(this); |
| 6 | + connect(ui.open_terminals, SIGNAL(clicked()), this, SLOT(open_terminal())); |
| 7 | + connect(ui.open_garmms, SIGNAL(clicked()), this, SLOT(open_grammars())); |
| 8 | + connect(ui.ok, SIGNAL(clicked()), this, SLOT(ok_btn())); |
| 9 | + connect(this, SIGNAL(file_type_send(int)), this, SLOT(open_file(int))); |
| 10 | + read_setting(); |
| 11 | +} |
| 12 | + |
| 13 | + |
| 14 | +void Config::connect_signal_slot() { |
| 15 | + connect(ui.open_terminals, SIGNAL(clicked()), this, SLOT(open_terminal())); |
| 16 | + connect(ui.open_garmms, SIGNAL(clicked()), this, SLOT(open_grammars())); |
| 17 | + connect(ui.ok, SIGNAL(clicked()), this, SLOT(ok_btn())); |
| 18 | + connect(this, SIGNAL(file_type_send(int)), this, SLOT(open_file(int))); |
| 19 | +} |
| 20 | + |
| 21 | +QString Config::get_grammars_path() { |
| 22 | + return ui.garmmars_path->text(); |
| 23 | +} |
| 24 | + |
| 25 | +QString Config::get_terminal_path() { |
| 26 | + return ui.terminals_path->text(); |
| 27 | +} |
| 28 | + |
| 29 | +Config::~Config() { |
| 30 | +} |
| 31 | + |
| 32 | +void Config::open_file(int file_type) { |
| 33 | + qDebug() << "open"; |
| 34 | + QFileDialog *file_dialog = new QFileDialog(this); //定义文 件对话框类 |
| 35 | + file_dialog->setWindowTitle(QString::fromLocal8Bit("打开文件")); //定义文件对话框标题 |
| 36 | + file_dialog->setDirectory("."); //设置默认文件路径 |
| 37 | + file_dialog->setNameFilter(tr("file(*.txt)")); //设置文件过滤器 |
| 38 | + file_dialog->setFileMode(QFileDialog::ExistingFile); //单个文件 |
| 39 | + file_dialog->setViewMode(QFileDialog::Detail); //设置视图模式 |
| 40 | + QString file_path; |
| 41 | + if (file_dialog->exec()) { |
| 42 | + QByteArray file_name_btye = file_dialog->selectedFiles()[0].toLocal8Bit(); |
| 43 | + std::string file_name = file_name_btye.toStdString(); |
| 44 | + file_path = QFileInfo(QString::fromStdString(file_name)).absoluteFilePath(); |
| 45 | + if (file_type == 0) { |
| 46 | + teminanls_path = file_path; |
| 47 | + ui.terminals_path->setText(file_path); |
| 48 | + |
| 49 | + } else { |
| 50 | + grammars_path = file_path; |
| 51 | + ui.garmmars_path->setText(file_path); |
| 52 | + } |
| 53 | + |
| 54 | + } |
| 55 | + delete file_dialog; |
| 56 | +} |
| 57 | + |
| 58 | +void Config::read_setting() { |
| 59 | + QSettings *read = new QSettings("config.ini", QSettings::IniFormat); |
| 60 | + QString file_name = QFileInfo(QString::fromLocal8Bit(read->value("/path/terminals").toByteArray())).absoluteFilePath(); |
| 61 | + ui.terminals_path->setText(file_name); |
| 62 | + file_name = QFileInfo(QString::fromLocal8Bit(read->value("/path/grammars").toByteArray())).absoluteFilePath(); |
| 63 | + ui.garmmars_path->setText(file_name); |
| 64 | + delete read; |
| 65 | +} |
| 66 | + |
| 67 | +void Config::wirte_setting() { |
| 68 | + QSettings *write = new QSettings("config.ini", QSettings::IniFormat); |
| 69 | + QString file_name = ui.terminals_path->text(); |
| 70 | + write->setValue("/path/terminals", file_name); |
| 71 | + file_name = ui.garmmars_path->text(); |
| 72 | + write->setValue("/path/grammars", file_name); |
| 73 | + delete write; |
| 74 | +} |
| 75 | + |
| 76 | +void Config::open_terminal() { |
| 77 | + emit file_type_send(0); |
| 78 | +} |
| 79 | + |
| 80 | +void Config::open_grammars() { |
| 81 | + emit file_type_send(1); |
| 82 | +} |
| 83 | + |
| 84 | +void Config::ok_btn() { |
| 85 | + emit finish(); |
| 86 | + emit teminanls_file(ui.terminals_path->text()); |
| 87 | + emit grammars_file(ui.garmmars_path->text()); |
| 88 | + wirte_setting(); |
| 89 | + |
| 90 | +} |
0 commit comments