File indexing completed on 2024-12-15 04:54:44

0001 /******************************************************************************
0002  *
0003  *  SPDX-FileCopyrightText: 2008 Szymon Tomasz Stefanek <pragma@kvirc.net>
0004  *
0005  *  SPDX-License-Identifier: GPL-2.0-or-later
0006  *
0007  *******************************************************************************/
0008 
0009 #include "utils/aggregationeditor.h"
0010 #include "core/aggregation.h"
0011 #include "utils/comboboxutils.h"
0012 
0013 #include <KTextEdit>
0014 #include <QCheckBox>
0015 #include <QGridLayout>
0016 #include <QLabel>
0017 #include <QLineEdit>
0018 #include <QPushButton>
0019 
0020 #include <KLocalizedString>
0021 #include <QComboBox>
0022 
0023 using namespace MessageList::Utils;
0024 using namespace MessageList::Core;
0025 
0026 AggregationEditor::AggregationEditor(QWidget *parent)
0027     : OptionSetEditor(parent)
0028 {
0029     // Grouping and Threading tab
0030     auto tab = new QWidget(this);
0031     addTab(tab, i18n("Groups && Threading"));
0032 
0033     auto tabg = new QGridLayout(tab);
0034 
0035     tabg->addWidget(new QLabel(i18n("Grouping:"), tab), 0, 0);
0036     mGroupingCombo = new QComboBox(tab);
0037     tabg->addWidget(mGroupingCombo, 0, 1);
0038 
0039     connect(mGroupingCombo, &QComboBox::activated, this, &AggregationEditor::groupingComboActivated);
0040 
0041     tabg->addWidget(new QLabel(i18n("Group expand policy:"), tab), 3, 0);
0042     mGroupExpandPolicyCombo = new QComboBox(tab);
0043     tabg->addWidget(mGroupExpandPolicyCombo, 3, 1);
0044 
0045     tabg->addWidget(new QLabel(i18n("Threading:"), tab), 4, 0);
0046     mThreadingCombo = new QComboBox(tab);
0047     tabg->addWidget(mThreadingCombo, 4, 1);
0048 
0049     connect(mThreadingCombo, &QComboBox::activated, this, &AggregationEditor::threadingComboActivated);
0050 
0051     tabg->addWidget(new QLabel(i18n("Thread leader:"), tab), 5, 0);
0052     mThreadLeaderCombo = new QComboBox(tab);
0053     tabg->addWidget(mThreadLeaderCombo, 5, 1);
0054 
0055     tabg->addWidget(new QLabel(i18n("Thread expand policy:"), tab), 6, 0);
0056     mThreadExpandPolicyCombo = new QComboBox(tab);
0057     tabg->addWidget(mThreadExpandPolicyCombo, 6, 1);
0058 
0059     tabg->setColumnStretch(1, 1);
0060     tabg->setRowStretch(9, 1);
0061 
0062     // Advanced tab
0063     tab = new QWidget(this);
0064     addTab(tab, i18nc("@title:tab Advanced settings tab for aggregation mode", "Advanced"));
0065 
0066     tabg = new QGridLayout(tab);
0067 
0068     tabg->addWidget(new QLabel(i18n("Fill view strategy:"), tab), 0, 0);
0069     mFillViewStrategyCombo = new QComboBox(tab);
0070     tabg->addWidget(mFillViewStrategyCombo, 0, 1);
0071 
0072     tabg->setColumnStretch(1, 1);
0073     tabg->setRowStretch(1, 1);
0074     fillGroupingCombo();
0075     fillThreadingCombo();
0076     fillFillViewStrategyCombo();
0077 
0078     fillThreadLeaderCombo();
0079     fillThreadExpandPolicyCombo();
0080     fillGroupExpandPolicyCombo();
0081 }
0082 
0083 AggregationEditor::~AggregationEditor() = default;
0084 
0085 void AggregationEditor::editAggregation(Aggregation *set)
0086 {
0087     mCurrentAggregation = set;
0088 
0089     if (!mCurrentAggregation) {
0090         setEnabled(false);
0091         return;
0092     }
0093     setEnabled(true);
0094     nameEdit()->setText(set->name());
0095     descriptionEdit()->setText(set->description());
0096 
0097     ComboBoxUtils::setIntegerOptionComboValue(mGroupingCombo, (int)mCurrentAggregation->grouping());
0098     ComboBoxUtils::setIntegerOptionComboValue(mThreadingCombo, (int)mCurrentAggregation->threading());
0099     ComboBoxUtils::setIntegerOptionComboValue(mFillViewStrategyCombo, (int)mCurrentAggregation->fillViewStrategy());
0100 
0101     // Necessary to fill after apply mGroupingCombo/mThreadingCombo/mFillViewStrategyCombo otherwise other combo are not filled.
0102     fillThreadLeaderCombo();
0103     fillThreadExpandPolicyCombo();
0104     fillGroupExpandPolicyCombo();
0105 
0106     ComboBoxUtils::setIntegerOptionComboValue(mThreadLeaderCombo, (int)mCurrentAggregation->threadLeader());
0107 
0108     ComboBoxUtils::setIntegerOptionComboValue(mThreadExpandPolicyCombo, (int)mCurrentAggregation->threadExpandPolicy());
0109 
0110     ComboBoxUtils::setIntegerOptionComboValue(mGroupExpandPolicyCombo, (int)mCurrentAggregation->groupExpandPolicy());
0111     fillThreadLeaderCombo();
0112     fillThreadExpandPolicyCombo();
0113     fillGroupExpandPolicyCombo();
0114 
0115     setReadOnly(mCurrentAggregation->readOnly());
0116 }
0117 
0118 void AggregationEditor::setReadOnly(bool readOnly)
0119 {
0120     mGroupingCombo->setEnabled(!readOnly);
0121     mGroupExpandPolicyCombo->setEnabled(!readOnly);
0122     mThreadingCombo->setEnabled(!readOnly);
0123     mThreadLeaderCombo->setEnabled(!readOnly);
0124     mThreadExpandPolicyCombo->setEnabled(!readOnly);
0125     mFillViewStrategyCombo->setEnabled(!readOnly);
0126 
0127     OptionSetEditor::setReadOnly(readOnly);
0128 }
0129 
0130 void AggregationEditor::commit()
0131 {
0132     mCurrentAggregation->setName(nameEdit()->text());
0133     mCurrentAggregation->setDescription(descriptionEdit()->toPlainText());
0134 
0135     mCurrentAggregation->setGrouping(static_cast<Aggregation::Grouping>(ComboBoxUtils::getIntegerOptionComboValue(mGroupingCombo, 0)));
0136 
0137     mCurrentAggregation->setGroupExpandPolicy(
0138         static_cast<Aggregation::GroupExpandPolicy>(ComboBoxUtils::getIntegerOptionComboValue(mGroupExpandPolicyCombo, 0)));
0139 
0140     mCurrentAggregation->setThreading(static_cast<Aggregation::Threading>(ComboBoxUtils::getIntegerOptionComboValue(mThreadingCombo, 0)));
0141 
0142     mCurrentAggregation->setThreadLeader(static_cast<Aggregation::ThreadLeader>(ComboBoxUtils::getIntegerOptionComboValue(mThreadLeaderCombo, 0)));
0143 
0144     mCurrentAggregation->setThreadExpandPolicy(
0145         static_cast<Aggregation::ThreadExpandPolicy>(ComboBoxUtils::getIntegerOptionComboValue(mThreadExpandPolicyCombo, 0)));
0146 
0147     mCurrentAggregation->setFillViewStrategy(static_cast<Aggregation::FillViewStrategy>(ComboBoxUtils::getIntegerOptionComboValue(mFillViewStrategyCombo, 0)));
0148 }
0149 
0150 void AggregationEditor::slotNameEditTextEdited(const QString &newName)
0151 {
0152     if (!mCurrentAggregation) {
0153         return;
0154     }
0155     mCurrentAggregation->setName(newName);
0156     Q_EMIT aggregationNameChanged();
0157 }
0158 
0159 void AggregationEditor::fillGroupingCombo()
0160 {
0161     ComboBoxUtils::fillIntegerOptionCombo(mGroupingCombo, Aggregation::enumerateGroupingOptions());
0162 }
0163 
0164 void AggregationEditor::groupingComboActivated(int)
0165 {
0166     fillGroupExpandPolicyCombo();
0167     fillThreadLeaderCombo();
0168 }
0169 
0170 void AggregationEditor::fillGroupExpandPolicyCombo()
0171 {
0172     ComboBoxUtils::fillIntegerOptionCombo(mGroupExpandPolicyCombo,
0173                                           Aggregation::enumerateGroupExpandPolicyOptions(
0174                                               (Aggregation::Grouping)ComboBoxUtils::getIntegerOptionComboValue(mGroupingCombo, Aggregation::NoGrouping)));
0175 }
0176 
0177 void AggregationEditor::fillThreadingCombo()
0178 {
0179     ComboBoxUtils::fillIntegerOptionCombo(mThreadingCombo, Aggregation::enumerateThreadingOptions());
0180 }
0181 
0182 void AggregationEditor::threadingComboActivated(int)
0183 {
0184     fillThreadLeaderCombo();
0185     fillThreadExpandPolicyCombo();
0186 }
0187 
0188 void AggregationEditor::fillThreadLeaderCombo()
0189 {
0190     ComboBoxUtils::fillIntegerOptionCombo(mThreadLeaderCombo,
0191                                           Aggregation::enumerateThreadLeaderOptions(
0192                                               (Aggregation::Grouping)ComboBoxUtils::getIntegerOptionComboValue(mGroupingCombo, Aggregation::NoGrouping),
0193                                               (Aggregation::Threading)ComboBoxUtils::getIntegerOptionComboValue(mThreadingCombo, Aggregation::NoThreading)));
0194 }
0195 
0196 void AggregationEditor::fillThreadExpandPolicyCombo()
0197 {
0198     ComboBoxUtils::fillIntegerOptionCombo(mThreadExpandPolicyCombo,
0199                                           Aggregation::enumerateThreadExpandPolicyOptions(
0200                                               (Aggregation::Threading)ComboBoxUtils::getIntegerOptionComboValue(mThreadingCombo, Aggregation::NoThreading)));
0201 }
0202 
0203 void AggregationEditor::fillFillViewStrategyCombo()
0204 {
0205     ComboBoxUtils::fillIntegerOptionCombo(mFillViewStrategyCombo, Aggregation::enumerateFillViewStrategyOptions());
0206 }
0207 
0208 #include "moc_aggregationeditor.cpp"