"Fossies" - the Fresh Open Source Software Archive

Member "cutter-1.10.3/src/dialogs/preferences/ColorThemeEditDialog.cpp" (8 May 2020, 5838 Bytes) of package /linux/privat/cutter-1.10.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ColorThemeEditDialog.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.10.2_vs_1.10.3.

    1 #include "ColorThemeEditDialog.h"
    2 #include "ui_ColorThemeEditDialog.h"
    3 
    4 #include "common/ColorThemeWorker.h"
    5 #include "common/Configuration.h"
    6 
    7 #include "widgets/ColorThemeListView.h"
    8 #include "widgets/DisassemblyWidget.h"
    9 
   10 #include <QScreen>
   11 #include <QKeyEvent>
   12 #include <QSortFilterProxyModel>
   13 
   14 ColorThemeEditDialog::ColorThemeEditDialog(QWidget *parent) :
   15     QDialog(parent),
   16     ui(new Ui::ColorThemeEditDialog),
   17     configSignalBlocker(Config()), // Blocks signals from Config to avoid updating of widgets during editing
   18     colorTheme(Config()->getColorTheme())
   19 {
   20     showAlphaOptions = {
   21         "gui.overview.border",
   22         "gui.overview.fill",
   23         "wordHighlight",
   24         "lineHighlight"
   25     };
   26     ui->setupUi(this);
   27     ui->colorComboBox->setShowOnlyCustom(true);
   28 
   29     previewDisasmWidget = new DisassemblyWidget(nullptr);
   30     previewDisasmWidget->setObjectName("Preview Disasm");
   31     previewDisasmWidget->setPreviewMode(true);
   32 #if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
   33     // default size limit is acceptable
   34     previewDisasmWidget->setMinimumSize(qApp->screenAt(previewDisasmWidget->pos())->size() * 0.5);
   35 #endif
   36     previewDisasmWidget->setWindowTitle(tr("Disassembly Preview"));
   37     previewDisasmWidget->setFeatures(QDockWidget::NoDockWidgetFeatures);
   38     ui->colorPickerAndPreviewLayout->addWidget(previewDisasmWidget);
   39 
   40 
   41     connect(ui->colorThemeListView, &ColorThemeListView::blink,
   42             previewDisasmWidget, &DisassemblyWidget::colorsUpdatedSlot);
   43 
   44     connect(ui->colorThemeListView, &ColorThemeListView::itemChanged,
   45             this, [this](const QColor& color) {
   46         ui->colorPicker->updateColor(color);
   47         QString optionName = ui->colorThemeListView->currentIndex()
   48                              .data(Qt::UserRole)
   49                              .value<ColorOption>()
   50                              .optionName;
   51         ui->colorPicker->setAlphaEnabled(showAlphaOptions.contains(optionName));
   52     });
   53 
   54     connect(ui->filterLineEdit, &QLineEdit::textChanged, this,
   55             [this](const QString& s) {
   56         static_cast<QSortFilterProxyModel*>(ui->colorThemeListView->model())->setFilterFixedString(s);
   57     });
   58 
   59     ui->colorThemeListView->setCurrentIndex(ui->colorThemeListView->model()->index(0, 0));
   60 
   61     connect(ui->colorPicker, &ColorPicker::colorChanged, this, &ColorThemeEditDialog::colorOptionChanged);
   62 
   63     connect(ui->colorComboBox, &ColorThemeComboBox::currentTextChanged,
   64             this, &ColorThemeEditDialog::editThemeChanged);
   65 }
   66 
   67 ColorThemeEditDialog::~ColorThemeEditDialog()
   68 {
   69     delete ui;
   70     previewDisasmWidget->deleteLater();
   71 }
   72 
   73 void ColorThemeEditDialog::accept()
   74 {
   75     colorTheme = Config()->getColorTheme();
   76     QJsonDocument sch = ui->colorThemeListView->colorSettingsModel()->getTheme();
   77     if (ThemeWorker().isCustomTheme(colorTheme)) {
   78         QString err = ThemeWorker().save(sch, colorTheme);
   79         if (!err.isEmpty()) {
   80             QMessageBox::critical(this, tr("Error"), err);
   81             return;
   82         }
   83     }
   84 
   85     configSignalBlocker.unblock();
   86     Config()->setColorTheme(colorTheme);
   87 
   88     QDialog::accept();
   89 }
   90 
   91 void ColorThemeEditDialog::reject()
   92 {
   93     if (themeWasEdited(ui->colorComboBox->currentText()) &&
   94         QMessageBox::question(this,
   95                               tr("Unsaved changes"),
   96                               tr("Are you sure you want to exit without saving? "
   97                                  "All changes will be lost.")) == QMessageBox::No) {
   98         return;
   99     }
  100     configSignalBlocker.unblock();
  101     Config()->setColorTheme(colorTheme);
  102 
  103     QDialog::reject();
  104 }
  105 
  106 void ColorThemeEditDialog::keyPressEvent(QKeyEvent *event)
  107 {
  108     switch (event->key()) {
  109     case Qt::Key_Escape:
  110         if (ui->colorPicker->isPickingFromScreen()) {
  111             ui->colorPicker->stopPickingFromScreen();
  112         }
  113         // fallthrough
  114     case Qt::Key_Return:
  115         event->accept();
  116         return;
  117     default:
  118         QDialog::keyPressEvent(event);
  119     }
  120 }
  121 
  122 void ColorThemeEditDialog::colorOptionChanged(const QColor& newColor)
  123 {
  124     QModelIndex currIndex = ui->colorThemeListView->currentIndex();
  125 
  126     if (!currIndex.isValid()) {
  127         return;
  128     }
  129 
  130     ColorOption currOption = currIndex.data(Qt::UserRole).value<ColorOption>();
  131     currOption.color = newColor;
  132     currOption.changed = true;
  133     ui->colorThemeListView->model()->setData(currIndex, QVariant::fromValue(currOption));
  134 
  135     Config()->setColor(currOption.optionName, currOption.color);
  136     if (!ColorThemeWorker::cutterSpecificOptions.contains(currOption.optionName)) {
  137         Core()->cmdRaw(QString("ec %1 %2").arg(currOption.optionName).arg(currOption.color.name()));
  138     }
  139     previewDisasmWidget->colorsUpdatedSlot();
  140 }
  141 
  142 void ColorThemeEditDialog::editThemeChanged(const QString& newTheme)
  143 {
  144     if (themeWasEdited(colorTheme)) {
  145         int ret = QMessageBox::question(this,
  146                               tr("Unsaved changes"),
  147                               tr("Are you sure you want to exit without saving? "
  148                                  "All changes will be lost."));
  149         if (ret == QMessageBox::No) {
  150             QSignalBlocker s(ui->colorComboBox); // avoid second call of this func
  151             int index = ui->colorComboBox->findText(colorTheme);
  152             index = index == -1 ? 0 : index;
  153             ui->colorComboBox->setCurrentIndex(index);
  154             Config()->setColorTheme(colorTheme);
  155             return;
  156         }
  157     }
  158     colorTheme = newTheme;
  159     ui->colorThemeListView->colorSettingsModel()->updateTheme();
  160     previewDisasmWidget->colorsUpdatedSlot();
  161     setWindowTitle(tr("Theme Editor - <%1>").arg(colorTheme));
  162 }
  163 
  164 bool ColorThemeEditDialog::themeWasEdited(const QString& theme) const
  165 {
  166     auto model = ui->colorThemeListView->colorSettingsModel();
  167     return ThemeWorker().getTheme(theme) != model->getTheme();
  168 }