/* Copyright © 2015 Gluzskiy Alexandr (sss) This file is part of Unknown Download Manager (UDM). UDM is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version. UDM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with UDM. If not, see . */ #include "download_add_widget.h" #include #include #include #include #include #include #include #include #include #include "marked_class.h" download_add_widget::download_add_widget(std::list &modules_, QWidget *parent) : QWidget(parent), modules(modules_) { QGridLayout *l = new QGridLayout(this); layout_settings = new QGridLayout; //must be created before first entry added to combobox to avoid crash QLabel *lbl_modules = new QLabel(tr("module:")); QComboBox *combo_modules = new QComboBox; connect(combo_modules, SIGNAL(currentIndexChanged(QString)), this, SLOT(combo_modules_currentIndexChanged(QString))); QHBoxLayout *layout_modules = new QHBoxLayout; layout_modules->addWidget(lbl_modules); QSpacerItem *spacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding); layout_modules->addItem(spacer); layout_modules->addWidget(combo_modules); l->addLayout(layout_modules, 0, 0, 1, 3); for(auto m : modules) { if(m.type() == SERVER_MODULE_TYPE::SERVER_MODULE_DOWNLOADER) combo_modules->addItem(m.name().c_str()); } l->addLayout(layout_settings, 1, 0, 1, 3); spacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding); l->addItem(spacer, 2, 0, 1, 3); QPushButton *btn_ok = new QPushButton(tr("Ok")), *btn_cancel = new QPushButton(tr("Cancel")); connect(btn_ok, SIGNAL(clicked(bool)), this, SLOT(btn_ok_clicked())); connect(btn_cancel, SIGNAL(clicked(bool)), this, SLOT(btn_cancel_clicked())); QHBoxLayout *layout_buttons = new QHBoxLayout; layout_buttons->addWidget(btn_ok); spacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding); layout_buttons->addItem(spacer); layout_buttons->addWidget(btn_cancel); l->addLayout(layout_buttons, 3, 0, 1, 3); setLayout(l); } void download_add_widget::combo_modules_currentIndexChanged(QString module) { current_module = module; { int rows = layout_settings->rowCount(), columns = layout_settings->columnCount(); for(int i = 0; i < rows; i++) { for(int ii = 0; ii < columns; ii ++) delete layout_settings->itemAtPosition(i, ii); } } for (QLayoutItem *item = layout_settings->takeAt(0); item != 0; item = layout_settings->takeAt(0)) delete item; int row = 0; for(auto m : modules) { if(m.name().c_str() == module) { for(auto ui : m.download_creation_ui()) { if(ui.type() == MODULE_UI_ELEMENT_TYPE::UI_STR || ui.type() == MODULE_UI_ELEMENT_TYPE::UI_INTEGER) { QLabel *l = new QLabel(ui.name().c_str()); layout_settings->addWidget(l, row, 0); QSpacerItem *spacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding); layout_settings->addItem(spacer, row, 1); switch(ui.type()) { case MODULE_UI_ELEMENT_TYPE::UI_STR: { marked_class *edit = new marked_class; edit->set_mark(ui.id()); edit->set_type(widget_type_e::QLineEdit_t); layout_settings->addWidget(edit, row, 2); } break; case MODULE_UI_ELEMENT_TYPE::UI_INTEGER: { marked_class *edit = new marked_class; edit->set_mark(ui.id()); edit->set_type(widget_type_e::QSpinBox_t); layout_settings->addWidget(edit, row, 2); } break; default: break; } ++row; } } } } } void download_add_widget::btn_ok_clicked() { int rows = layout_settings->rowCount(); std::map settings; for (int i = 0; i < rows; ++i) { QWidget *w_ = layout_settings->itemAtPosition(i, 2)->widget(); marked_class *w = static_cast *>(w_); switch(w->get_type()) { case widget_type_e::QLineEdit_t: { marked_class *l = static_cast *>(w_); settings[l->get_mark()] = l->text().toStdString(); } break; case widget_type_e::QSpinBox_t: { marked_class *l = static_cast *>(w_); settings[l->get_mark()] = l->text().toStdString(); } break; default: break; } } emit got_download_settings(current_module.toStdString(), settings); close(); deleteLater(); } void download_add_widget::btn_cancel_clicked() { close(); deleteLater(); //? }