| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "services/abstract/gui/formcategorydetails.h" |
| 4 | |
| 5 | #include "3rd-party/boolinq/boolinq.h" |
| 6 | #include "database/databasequeries.h" |
| 7 | #include "definitions/definitions.h" |
| 8 | #include "exceptions/applicationexception.h" |
| 9 | #include "gui/dialogs/filedialog.h" |
| 10 | #include "gui/guiutilities.h" |
| 11 | #include "gui/reusable/baselineedit.h" |
| 12 | #include "miscellaneous/iconfactory.h" |
| 13 | #include "services/abstract/category.h" |
| 14 | #include "services/abstract/gui/multifeededitcheckbox.h" |
| 15 | #include "services/abstract/rootitem.h" |
| 16 | |
| 17 | #include "ui_formcategorydetails.h" |
| 18 | |
| 19 | #include <QAction> |
| 20 | #include <QDialogButtonBox> |
| 21 | #include <QImageReader> |
| 22 | #include <QLineEdit> |
| 23 | #include <QMenu> |
| 24 | #include <QPushButton> |
| 25 | #include <QTextEdit> |
| 26 | #include <QToolButton> |
| 27 | |
| 28 | FormCategoryDetails::FormCategoryDetails(ServiceRoot* service_root, RootItem* parent_to_select, QWidget* parent) |
| 29 | : QDialog(parent), m_serviceRoot(service_root), m_parentToSelect(parent_to_select) { |
| 30 | initialize(); |
| 31 | createConnections(); |
| 32 | |
| 33 | // Initialize text boxes. |
| 34 | onTitleChanged(QString()); |
| 35 | onDescriptionChanged(QString()); |
| 36 | } |
| 37 | |
| 38 | FormCategoryDetails::~FormCategoryDetails() { |
| 39 | qDebugNN << LOGSEC_GUI << "Destroying FormCategoryDetails instance."; |
| 40 | } |
| 41 | |
| 42 | void FormCategoryDetails::createConnections() { |
| 43 | // General connections. |
| 44 | connect(m_ui->m_buttonBox, &QDialogButtonBox::accepted, this, &FormCategoryDetails::apply); |
| 45 | connect(m_ui->m_txtTitle->lineEdit(), &BaseLineEdit::textChanged, this, &FormCategoryDetails::onTitleChanged); |
| 46 | connect(m_ui->m_txtDescription->lineEdit(), |
| 47 | &BaseLineEdit::textChanged, |
| 48 | this, |
| 49 | &FormCategoryDetails::onDescriptionChanged); |
| 50 | |
| 51 | // Icon connections. |
| 52 | connect(m_actionLoadIconFromFile, &QAction::triggered, this, &FormCategoryDetails::onLoadIconFromFile); |
| 53 | connect(m_actionUseDefaultIcon, &QAction::triggered, this, &FormCategoryDetails::onUseDefaultIcon); |
| 54 | } |
| 55 | |
| 56 | bool FormCategoryDetails::isChangeAllowed(MultiFeedEditCheckBox* mcb) const { |
| 57 | return !m_isBatchEdit || mcb->isChecked(); |
| 58 | } |
| 59 | |
| 60 | void FormCategoryDetails::loadCategoryData() { |
| 61 | Category* cat = category<Category>(); |
| 62 | |
| 63 | if (m_isBatchEdit) { |
| 64 | // We hook batch selectors. |
| 65 | m_ui->m_mcbParent->addActionWidget(m_ui->m_cmbParentCategory); |
| 66 | m_ui->m_mcbTitle->addActionWidget(m_ui->m_txtTitle); |
| 67 | m_ui->m_mcbDescription->addActionWidget(m_ui->m_txtDescription); |
| 68 | m_ui->m_mcbIcon->addActionWidget(m_ui->m_btnIcon); |
| 69 | } |
| 70 | else { |
| 71 | // We hide batch selectors. |
| 72 | for (auto* cb : findChildren<MultiFeedEditCheckBox*>()) { |
| 73 | cb->hide(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | loadCategories(m_serviceRoot->getSubTreeCategories(), m_serviceRoot, cat); |
| 78 | |
| 79 | if (m_creatingNew) { |
| 80 | GuiUtilities::applyDialogProperties(*this, qApp->icons()->fromTheme(QSL("folder")), tr("Add new category")); |
| 81 | |
| 82 | // Make sure that "default" icon is used as the default option for new |
| 83 | // categories. |
| 84 | m_actionUseDefaultIcon->trigger(); |
| 85 | |
| 86 | // Load parent from suggested item. |
| 87 | if (m_parentToSelect != nullptr) { |
| 88 | if (m_parentToSelect->kind() == RootItem::Kind::Category) { |
| 89 | m_ui->m_cmbParentCategory |
| 90 | ->setCurrentIndex(m_ui->m_cmbParentCategory->findData(QVariant::fromValue(m_parentToSelect))); |
| 91 | } |
| 92 | else if (m_parentToSelect->kind() == RootItem::Kind::Feed) { |
| 93 | int target_item = m_ui->m_cmbParentCategory->findData(QVariant::fromValue(m_parentToSelect->parent())); |
| 94 | |
| 95 | if (target_item >= 0) { |
| 96 | m_ui->m_cmbParentCategory->setCurrentIndex(target_item); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | else { |
| 102 | if (!m_isBatchEdit) { |
| 103 | GuiUtilities::applyDialogProperties(*this, cat->fullIcon(), tr("Edit \"%1\"").arg(cat->title())); |
| 104 | } |
| 105 | else { |
| 106 | GuiUtilities::applyDialogProperties(*this, |
| 107 | qApp->icons()->fromTheme(QSL("folder")), |
| 108 | tr("Edit %n categories", nullptr, m_categories.size())); |
| 109 | } |
| 110 | |
| 111 | m_ui->m_cmbParentCategory->setCurrentIndex(m_ui->m_cmbParentCategory->findData(QVariant::fromValue(cat->parent()))); |
| 112 | } |
| 113 | |
| 114 | m_ui->m_txtTitle->lineEdit()->setText(cat->title()); |
| 115 | m_ui->m_txtDescription->lineEdit()->setText(cat->description()); |
| 116 | m_ui->m_btnIcon->setIcon(cat->icon()); |
| 117 | |
| 118 | m_ui->m_txtTitle->lineEdit()->setFocus(); |
| 119 | } |
| 120 | |
| 121 | void FormCategoryDetails::apply() { |
| 122 | QList<Category*> cats = categories<Category>(); |
| 123 | RootItem* parent = m_ui->m_cmbParentCategory->currentData().value<RootItem*>(); |
| 124 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 125 | |
| 126 | for (Category* cat : cats) { |
| 127 | if (isChangeAllowed(m_ui->m_mcbTitle)) { |
| 128 | cat->setTitle(m_ui->m_txtTitle->lineEdit()->text()); |
| 129 | } |
| 130 | |
| 131 | if (isChangeAllowed(m_ui->m_mcbDescription)) { |
| 132 | cat->setDescription(m_ui->m_txtDescription->lineEdit()->text()); |
| 133 | } |
| 134 | |
| 135 | if (isChangeAllowed(m_ui->m_mcbIcon)) { |
| 136 | cat->setIcon(m_ui->m_btnIcon->icon()); |
| 137 | } |
| 138 | |
| 139 | int new_parent_id; |
| 140 | |
| 141 | if (isChangeAllowed(m_ui->m_mcbParent)) { |
| 142 | new_parent_id = parent->id(); |
| 143 | } |
| 144 | else { |
| 145 | new_parent_id = cat->parent()->id(); |
| 146 | } |
| 147 | |
| 148 | try { |
| 149 | DatabaseQueries::createOverwriteCategory(database, cat, m_serviceRoot->accountId(), new_parent_id); |
| 150 | } |
| 151 | catch (const ApplicationException& ex) { |
| 152 | qFatal("Cannot save category: '%s'.", qPrintable(ex.message())); |
| 153 | } |
| 154 | |
| 155 | if (isChangeAllowed(m_ui->m_mcbParent)) { |
| 156 | m_serviceRoot->requestItemReassignment(cat, parent); |
| 157 | } |
| 158 | |
| 159 | if (m_creatingNew) { |
| 160 | m_serviceRoot->requestItemExpand({parent}, true); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | m_serviceRoot->itemChanged(categories<RootItem>()); |
| 165 | accept(); |
| 166 | } |
| 167 | |
| 168 | void FormCategoryDetails::onTitleChanged(const QString& new_title) { |
| 169 | if (!new_title.simplified().isEmpty()) { |
| 170 | m_ui->m_buttonBox->button(QDialogButtonBox::StandardButton::Ok)->setEnabled(true); |
| 171 | m_ui->m_txtTitle->setStatus(WidgetWithStatus::StatusType::Ok, tr("Category name is ok.")); |
| 172 | } |
| 173 | else { |
| 174 | m_ui->m_buttonBox->button(QDialogButtonBox::StandardButton::Ok)->setEnabled(false); |
| 175 | m_ui->m_txtTitle->setStatus(WidgetWithStatus::StatusType::Error, tr("Category name is too short.")); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void FormCategoryDetails::onDescriptionChanged(const QString& new_description) { |
| 180 | if (new_description.simplified().isEmpty()) { |
| 181 | m_ui->m_txtDescription->setStatus(LineEditWithStatus::StatusType::Warning, tr("Description is empty.")); |
| 182 | } |
| 183 | else { |
| 184 | m_ui->m_txtDescription->setStatus(LineEditWithStatus::StatusType::Ok, tr("The description is ok.")); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void FormCategoryDetails::onLoadIconFromFile() { |
| 189 | auto supported_formats = QImageReader::supportedImageFormats(); |
| 190 | auto prefixed_formats = boolinq::from(supported_formats) |
| 191 | .select([](const QByteArray& frmt) { |
| 192 | return QSL("*.%1").arg(QString::fromLocal8Bit(frmt)); |
| 193 | }) |
| 194 | .toStdList(); |
| 195 | |
| 196 | QStringList list_formats = FROM_STD_LIST(QStringList, prefixed_formats); |
| 197 | QString fil = FileDialog::openFileName(this, |
| 198 | tr("Select icon file for the category"), |
| 199 | qApp->homeFolder(), |
| 200 | tr("Images (%1)").arg(list_formats.join(QL1C(' '))), |
| 201 | nullptr, |
| 202 | GENERAL_REMEMBERED_PATH); |
| 203 | |
| 204 | if (!fil.isEmpty()) { |
| 205 | m_ui->m_btnIcon->setIcon(QIcon(fil)); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | void FormCategoryDetails::onUseDefaultIcon() { |
| 210 | m_ui->m_btnIcon->setIcon(QIcon()); |
| 211 | } |
| 212 | |
| 213 | void FormCategoryDetails::initialize() { |
| 214 | m_ui.reset(new Ui::FormCategoryDetails()); |
| 215 | m_ui->setupUi(this); |
| 216 | |
| 217 | // Set text boxes. |
| 218 | m_ui->m_txtTitle->lineEdit()->setPlaceholderText(tr("Category title")); |
| 219 | m_ui->m_txtTitle->lineEdit()->setToolTip(tr("Set title for your category.")); |
| 220 | m_ui->m_txtDescription->lineEdit()->setPlaceholderText(tr("Category description")); |
| 221 | m_ui->m_txtDescription->lineEdit()->setToolTip(tr("Set description for your category.")); |
| 222 | |
| 223 | // Setup button box. |
| 224 | m_ui->m_buttonBox->button(QDialogButtonBox::StandardButton::Ok)->setEnabled(false); |
| 225 | |
| 226 | // Setup menu & actions for icon selection. |
| 227 | m_iconMenu = new QMenu(tr("Icon selection"), this); |
| 228 | m_actionLoadIconFromFile = |
| 229 | new QAction(qApp->icons()->fromTheme(QSL("image-x-generic")), tr("Load icon from file..."), this); |
| 230 | m_actionUseDefaultIcon = |
| 231 | new QAction(qApp->icons()->fromTheme(QSL("folder")), tr("Use default icon from icon theme"), this); |
| 232 | m_iconMenu->addAction(m_actionLoadIconFromFile); |
| 233 | m_iconMenu->addAction(m_actionUseDefaultIcon); |
| 234 | m_ui->m_btnIcon->setMenu(m_iconMenu); |
| 235 | |
| 236 | // Setup tab order. |
| 237 | setTabOrder(m_ui->m_cmbParentCategory, m_ui->m_txtTitle->lineEdit()); |
| 238 | setTabOrder(m_ui->m_txtTitle->lineEdit(), m_ui->m_txtDescription->lineEdit()); |
| 239 | setTabOrder(m_ui->m_txtDescription->lineEdit(), m_ui->m_btnIcon); |
| 240 | setTabOrder(m_ui->m_btnIcon, m_ui->m_buttonBox); |
| 241 | m_ui->m_txtTitle->lineEdit()->setFocus(Qt::FocusReason::TabFocusReason); |
| 242 | } |
| 243 | |
| 244 | void FormCategoryDetails::loadCategories(const QList<Category*>& categories, |
| 245 | RootItem* root_item, |
| 246 | Category* input_category) { |
| 247 | m_ui->m_cmbParentCategory->addItem(root_item->icon(), root_item->title(), QVariant::fromValue(root_item)); |
| 248 | |
| 249 | for (Category* category : categories) { |
| 250 | if (input_category != nullptr && (category == input_category || category->isChildOf(input_category))) { |
| 251 | // This category cannot be selected as the new |
| 252 | // parent for currently edited category, so |
| 253 | // don't add it. |
| 254 | continue; |
| 255 | } |
| 256 | |
| 257 | m_ui->m_cmbParentCategory->addItem(category->data(FDS_MODEL_TITLE_INDEX, Qt::DecorationRole).value<QIcon>(), |
| 258 | category->title(), |
| 259 | QVariant::fromValue(category)); |
| 260 | } |
| 261 | } |