RedPanda-CPP/RedPandaIDE/settingsdialog/editorcolorschemewidget.cpp

507 lines
18 KiB
C++
Raw Normal View History

2021-12-26 23:18:28 +08:00
/*
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
2021-06-14 08:01:00 +08:00
#include "editorcolorschemewidget.h"
#include "ui_editorcolorschemewidget.h"
2021-06-18 22:57:19 +08:00
#include "../settings.h"
#include "../colorscheme.h"
2021-06-20 14:30:47 +08:00
#include "../mainwindow.h"
#include <QAction>
#include <QMessageBox>
2021-06-20 14:49:38 +08:00
#include <QDebug>
2021-06-20 22:54:16 +08:00
#include <QInputDialog>
#include <QFileDialog>
2024-04-05 09:42:00 +08:00
#include <qsynedit/document.h>
2021-06-14 08:01:00 +08:00
2021-06-17 10:39:46 +08:00
EditorColorSchemeWidget::EditorColorSchemeWidget(const QString& name, const QString& group, QWidget *parent) :
SettingsWidget(name,group,parent),
2021-06-14 08:01:00 +08:00
ui(new Ui::EditorColorSchemeWidget)
{
ui->setupUi(this);
mStatementColors = std::make_shared<QHash<StatementKind, std::shared_ptr<ColorSchemeItem> >>();
2021-06-18 22:57:19 +08:00
mItemDelegate = new ColorSchemeItemDelegate(this);
ui->cbScheme->setItemDelegate(mItemDelegate);
2021-06-20 14:30:47 +08:00
mDefaultSchemeComboFont = ui->cbScheme->font();
mModifiedSchemeComboFont = mDefaultSchemeComboFont;
mModifiedSchemeComboFont.setBold(true);
int schemeCount=0;
2021-06-18 22:57:19 +08:00
for (QString schemeName: pColorManager->getSchemes()) {
2021-06-20 14:30:47 +08:00
PColorScheme scheme = pColorManager->get(schemeName);
if (!scheme)
return;
2021-06-18 22:57:19 +08:00
ui->cbScheme->addItem(schemeName);
2021-06-20 14:30:47 +08:00
if (scheme->customed())
ui->cbScheme->setItemData(schemeCount,mModifiedSchemeComboFont,Qt::FontRole);
else
ui->cbScheme->setItemData(schemeCount,mDefaultSchemeComboFont,Qt::FontRole);
2021-06-20 14:30:47 +08:00
schemeCount++;
2021-06-18 22:57:19 +08:00
}
QItemSelectionModel *m = ui->treeItems->selectionModel();
ui->treeItems->setModel(&mDefinesModel);
delete m;
mDefinesModel.setHorizontalHeaderLabels(QStringList());
for (QString defineName : pColorManager->getDefines()) {
addDefine(defineName, pColorManager->getDefine(defineName));
}
ui->treeItems->expandAll();
2021-06-20 14:30:47 +08:00
QModelIndex groupIndex = mDefinesModel.index(0,0);
QModelIndex index = mDefinesModel.index(0,0,groupIndex);
ui->treeItems->setCurrentIndex(index);
connect(ui->treeItems->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &EditorColorSchemeWidget::onItemSelectionChanged);
2021-06-20 14:30:47 +08:00
connect(ui->cbScheme, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &EditorColorSchemeWidget::changeSchemeComboFont);
connect(ui->cbScheme, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &EditorColorSchemeWidget::onItemSelectionChanged);
connect(this, &SettingsWidget::settingsChanged,this,
&EditorColorSchemeWidget::onSettingChanged);
ui->editDemo->setUseCodeFolding(true);
ui->editDemo->document()->setText(
"#include <iostream>\n"
"#include <conio.h>\n"
"\n"
"int x=10;\n"
"\n"
"int main(int argc, char **argv)\n"
"{\n"
" int numbers[20]; // warning line\n"
" float average, total; // bookmark\n"
" for (int i = 0; i <= 19; i++) // active breakpoint\n"
" { // breakpoint\n"
" numbers[i] = i+x;\n"
" Total += i; // error line\n"
" }\n"
" average = total / 20; // comment\n"
" std::cout << \"total: \" << total <<\n"
" \"\\nAverage: \" << average;\n"
" getch();\n"
"}\n"
);
ui->editDemo->setReadOnly(true);
ui->editDemo->toggleBreakpoint(11);
ui->editDemo->toggleBookmark(9);
ui->editDemo->addSyntaxIssues(13, 9, 14, CompileIssueType::Error, "[Error] 'Total' was not declared in this scope; did you mean 'total'?");
ui->editDemo->addSyntaxIssues(8, 9, 16, CompileIssueType::Warning, "[Warning] variable 'numbers' set but not used [-Wunused-but-set-variable]");
ui->editDemo->setCaretY(9);
ui->editDemo->setActiveBreakpointFocus(10,false);
ui->editDemo->reparseDocument();
ui->editDemo->invalidate();
2021-06-20 14:30:47 +08:00
onItemSelectionChanged();
}
void EditorColorSchemeWidget::addDefine(const QString& name, PColorSchemeItemDefine define)
{
QList<QStandardItem*> items = mDefinesModel.findItems(define->group());
QStandardItem* pGroupItem;
if (items.count() == 0 ) {
//delete in the destructor
pGroupItem = new QStandardItem(define->group());
pGroupItem->setData("", NameRole);
mDefinesModel.appendRow(pGroupItem);
} else {
pGroupItem = items[0];
}
//delete in the destructor
QStandardItem* pWidgetItem = new QStandardItem(define->displayName());
pWidgetItem->setData(name, NameRole);
pGroupItem->appendRow(pWidgetItem);
}
PColorSchemeItem EditorColorSchemeWidget::getCurrentItem()
{
QItemSelectionModel * selectionModel = ui->treeItems->selectionModel();
QString name =mDefinesModel.data(selectionModel->currentIndex(),NameRole).toString();
if (name.isEmpty())
return PColorSchemeItem();
return pColorManager->getItem(ui->cbScheme->currentText(), name);
2021-06-14 08:01:00 +08:00
}
2021-06-20 09:27:37 +08:00
PColorScheme EditorColorSchemeWidget::getCurrentScheme()
{
return pColorManager->get(ui->cbScheme->currentText());
}
2021-06-20 14:30:47 +08:00
void EditorColorSchemeWidget::connectModificationSlots()
{
connect(ui->cbBackground,&QCheckBox::stateChanged,
this, &EditorColorSchemeWidget::onBackgroundChanged);
connect(ui->colorBackground,&ColorEdit::colorChanged,
this, &EditorColorSchemeWidget::onBackgroundChanged);
connect(ui->cbForeground,&QCheckBox::stateChanged,
this, &EditorColorSchemeWidget::onForegroundChanged);
connect(ui->colorForeground,&ColorEdit::colorChanged,
this, &EditorColorSchemeWidget::onForegroundChanged);
connect(ui->cbBold,&QCheckBox::stateChanged,
this, &EditorColorSchemeWidget::onFontStyleChanged);
connect(ui->cbItalic,&QCheckBox::stateChanged,
this, &EditorColorSchemeWidget::onFontStyleChanged);
connect(ui->cbStrikeout,&QCheckBox::stateChanged,
this, &EditorColorSchemeWidget::onFontStyleChanged);
connect(ui->cbUnderlined,&QCheckBox::stateChanged,
this, &EditorColorSchemeWidget::onFontStyleChanged);
}
void EditorColorSchemeWidget::disconnectModificationSlots()
{
disconnect(ui->cbBackground,&QCheckBox::stateChanged,
this, &EditorColorSchemeWidget::onBackgroundChanged);
disconnect(ui->colorBackground,&ColorEdit::colorChanged,
this, &EditorColorSchemeWidget::onBackgroundChanged);
disconnect(ui->cbForeground,&QCheckBox::stateChanged,
this, &EditorColorSchemeWidget::onForegroundChanged);
disconnect(ui->colorForeground,&ColorEdit::colorChanged,
this, &EditorColorSchemeWidget::onForegroundChanged);
disconnect(ui->cbBold,&QCheckBox::stateChanged,
this, &EditorColorSchemeWidget::onFontStyleChanged);
disconnect(ui->cbItalic,&QCheckBox::stateChanged,
this, &EditorColorSchemeWidget::onFontStyleChanged);
disconnect(ui->cbStrikeout,&QCheckBox::stateChanged,
this, &EditorColorSchemeWidget::onFontStyleChanged);
disconnect(ui->cbUnderlined,&QCheckBox::stateChanged,
this, &EditorColorSchemeWidget::onFontStyleChanged);
}
void EditorColorSchemeWidget::setCurrentSchemeModified()
{
PColorScheme scheme = getCurrentScheme();
if (scheme) {
scheme->setCustomed(true);
}
// if (mModifiedSchemes.contains(ui->cbScheme->currentText()))
// return;
2021-06-20 14:30:47 +08:00
mModifiedSchemes.insert(ui->cbScheme->currentText());
ui->cbScheme->setItemData(ui->cbScheme->currentIndex(),
mModifiedSchemeComboFont,Qt::FontRole);
ui->cbScheme->setFont(mModifiedSchemeComboFont);
2021-06-20 22:54:16 +08:00
//ui->cbScheme->view()->setFont(mDefaultSchemeComboFont);
2021-06-20 14:30:47 +08:00
//we must reset the editor here, because this slot is processed after the onSettingChanged
onSettingChanged();
}
2021-06-14 08:01:00 +08:00
EditorColorSchemeWidget::~EditorColorSchemeWidget()
{
delete ui;
//mDefinesModel.clear();
2021-06-14 08:01:00 +08:00
}
2021-06-18 21:48:40 +08:00
static void setColorProp(ColorEdit* ce, QCheckBox* cb, const QColor& color) {
if (color.isValid()) {
cb->setChecked(true);
ce->setColor(color);
ce->setVisible(true);
} else {
cb->setChecked(false);
ce->setVisible(false);
}
}
void EditorColorSchemeWidget::onItemSelectionChanged()
{
2021-06-20 14:30:47 +08:00
disconnectModificationSlots();
QItemSelectionModel * selectionModel = ui->treeItems->selectionModel();
QString name =mDefinesModel.data(selectionModel->currentIndex(),NameRole).toString();
bool found = false;
if (!name.isEmpty()) {
PColorSchemeItemDefine define = pColorManager->getDefine(name);
if (define) {
found = true;
ui->cbBackground->setEnabled(define->hasBackground());
ui->colorBackground->setEnabled(define->hasBackground());
ui->cbForeground->setEnabled(define->hasForeground());
ui->colorForeground->setEnabled(define->hasForeground());
ui->grpFontStyles->setEnabled(define->hasFontStyle());
PColorSchemeItem item = pColorManager->getItem(ui->cbScheme->currentText(), name);
if (!item) {
PColorScheme scheme = pColorManager->get(ui->cbScheme->currentText());
if (scheme) {
scheme->addItem(name);
}
}
if (define->hasBackground() && item) {
setColorProp(ui->colorBackground, ui->cbBackground,item->background());
} else {
setColorProp(ui->colorBackground, ui->cbBackground,QColor());
}
if (define->hasForeground() && item) {
setColorProp(ui->colorForeground, ui->cbForeground,item->foreground());
} else {
setColorProp(ui->colorForeground, ui->cbForeground,QColor());
}
if (define->hasFontStyle() && item) {
ui->cbBold->setChecked(item->bold());
ui->cbItalic->setChecked(item->italic());
ui->cbUnderlined->setChecked(item->underlined());
ui->cbStrikeout->setChecked(item->strikeout());
} else {
ui->cbBold->setChecked(false);
ui->cbItalic->setChecked(false);
ui->cbUnderlined->setChecked(false);
ui->cbStrikeout->setChecked(false);
}
}
}
2021-06-20 14:30:47 +08:00
ui->widgetSchemeItem->setEnabled(found);
2021-06-20 14:30:47 +08:00
connectModificationSlots();
}
void EditorColorSchemeWidget::onSettingChanged()
{
pColorManager->updateStatementColors(mStatementColors,ui->cbScheme->currentText());
ui->editDemo->applyColorScheme(ui->cbScheme->currentText());
ui->editDemo->setStatementColors(mStatementColors);
}
void EditorColorSchemeWidget::onForegroundChanged()
{
PColorSchemeItem item = getCurrentItem();
if (!item)
return;
2021-06-20 22:54:16 +08:00
ui->colorForeground->setVisible(ui->cbForeground->isChecked());
if (ui->cbForeground->isChecked()) {
item->setForeground(ui->colorForeground->color());
} else {
2021-06-20 22:54:16 +08:00
ui->colorForeground->setColor(QColor());
item->setForeground(QColor());
}
2021-06-20 14:30:47 +08:00
setCurrentSchemeModified();
}
void EditorColorSchemeWidget::onBackgroundChanged()
{
PColorSchemeItem item = getCurrentItem();
if (!item)
return;
2021-06-20 22:54:16 +08:00
ui->colorBackground->setVisible(ui->cbBackground->isChecked());
if (ui->cbBackground->isChecked()) {
item->setBackground(ui->colorBackground->color());
} else {
2021-06-20 22:54:16 +08:00
ui->colorBackground->setColor(QColor());
item->setBackground(QColor());
}
2021-06-20 14:30:47 +08:00
setCurrentSchemeModified();
}
void EditorColorSchemeWidget::onFontStyleChanged()
{
PColorSchemeItem item = getCurrentItem();
if (!item)
return;
item->setBold(ui->cbBold->isChecked());
item->setItalic(ui->cbItalic->isChecked());
item->setStrikeout(ui->cbStrikeout->isChecked());
item->setUnderlined(ui->cbUnderlined->isChecked());
2021-06-20 14:30:47 +08:00
setCurrentSchemeModified();
}
void EditorColorSchemeWidget::changeSchemeComboFont()
{
QString name = ui->cbScheme->currentText();
PColorScheme scheme = pColorManager->get(name);
if (scheme && scheme->customed()) {
ui->cbScheme->setFont(mModifiedSchemeComboFont);
} else {
ui->cbScheme->setFont(mDefaultSchemeComboFont);
2021-06-20 09:27:37 +08:00
}
2021-06-20 22:54:16 +08:00
//ui->cbScheme->view()->setFont(mDefaultSchemeComboFont);
}
2021-06-18 21:48:40 +08:00
void EditorColorSchemeWidget::doLoad()
{
2021-06-20 14:30:47 +08:00
ui->cbScheme->setCurrentText(pSettings->editor().colorScheme());
changeSchemeComboFont();
ui->chkRainborParenthesis->setChecked(pSettings->editor().rainbowParenthesis());
2021-06-18 21:48:40 +08:00
}
void EditorColorSchemeWidget::doSave()
2021-06-20 14:30:47 +08:00
{
try {
for (QString name:mModifiedSchemes) {
pColorManager->saveScheme(name);
}
pSettings->editor().setColorScheme(ui->cbScheme->currentText());
pSettings->editor().setRainbowParenthesis(ui->chkRainborParenthesis->isChecked());
2021-06-20 14:30:47 +08:00
pSettings->editor().save();
pMainWindow->updateEditorColorSchemes();
} catch (FileError e) {
QMessageBox::critical(this,tr("Error"),e.reason());
2021-06-20 14:30:47 +08:00
}
}
void EditorColorSchemeWidget::on_actionCopy_Scheme_triggered()
2021-06-18 21:48:40 +08:00
{
2021-06-20 14:49:38 +08:00
QString newName = pColorManager->copy(ui->cbScheme->currentText());
if (newName.isEmpty())
return;
ui->cbScheme->addItem(newName);
ui->cbScheme->setCurrentText(newName);
2021-06-18 21:48:40 +08:00
}
2021-06-20 14:30:47 +08:00
void EditorColorSchemeWidget::on_btnSchemeMenu_pressed()
{
QMenu menu;
2021-06-20 14:49:38 +08:00
PColorScheme scheme = getCurrentScheme();
2021-06-20 14:30:47 +08:00
if (scheme) {
if (scheme->customed()) {
menu.addAction(ui->actionReset_Scheme);
}
if (!scheme->bundled()) {
menu.addAction(ui->actionRename_Scheme);
menu.addAction(ui->actionDelete_Scheme);
}
2021-06-20 22:54:16 +08:00
QString name = ui->cbScheme->currentText();
if (!pColorManager->exists(name+ " Copy"))
menu.addAction(ui->actionCopy_Scheme);
2021-06-20 14:30:47 +08:00
menu.addAction(ui->actionExport_Scheme);
menu.addSeparator();
}
menu.addAction(ui->actionImport_Scheme);
QPoint p;
p.setX(0);
p.setY(ui->btnSchemeMenu->height()+2);
2021-06-20 14:49:38 +08:00
menu.exec(ui->btnSchemeMenu->mapToGlobal(p));
2021-06-20 14:30:47 +08:00
}
2021-06-20 22:54:16 +08:00
void EditorColorSchemeWidget::on_actionImport_Scheme_triggered()
{
QString filename = QFileDialog::getOpenFileName(this,
tr("Open"), QString(), tr("Color Scheme Files (*.scheme)"));
if (filename.isEmpty())
return;
QFileInfo fileInfo(filename);
QString name = fileInfo.fileName();
QString suffix = EXT_COLOR_SCHEME;
if (!name.toLower().endsWith(suffix))
return;
name.remove(name.length()-suffix.length(),suffix.length());
name.replace('_',' ');
if (!pColorManager->isValidName(name)) {
QMessageBox::critical(this,tr("Error"),tr("'%1' is not a valid name for color scheme file."));
2021-06-20 22:54:16 +08:00
return;
}
try {
PColorScheme scheme = ColorScheme::load(filename);
pColorManager->add(name, scheme);
ui->cbScheme->addItem(name);
ui->cbScheme->setCurrentText(name);
} catch (FileError e) {
QMessageBox::critical(this,tr("Error"),e.reason());
2021-06-20 22:54:16 +08:00
return;
}
}
void EditorColorSchemeWidget::on_actionRename_Scheme_triggered()
{
QString name = ui->cbScheme->currentText();
bool isOk;
QString newName = QInputDialog::getText(this,tr("New scheme name"),tr("New scheme name"),
QLineEdit::Normal,name,&isOk);
if (isOk) {
if (!pColorManager->isValidName(newName)) {
QMessageBox::critical(this,tr("Error"),tr("'%1' is not a valid scheme name!").arg(newName));
2021-06-20 22:54:16 +08:00
return;
}
try {
pColorManager->rename(name,newName);
ui->cbScheme->setItemText(
ui->cbScheme->currentIndex(),
newName
);
if (mModifiedSchemes.contains(name))
mModifiedSchemes.remove(name);
mModifiedSchemes.insert(newName);
} catch(FileError e) {
QMessageBox::critical(this,tr("Error"),e.reason());
2021-06-20 22:54:16 +08:00
}
}
}
void EditorColorSchemeWidget::on_actionReset_Scheme_triggered()
{
try {
if (pColorManager->restoreToDefault(ui->cbScheme->currentText())) {
ui->cbScheme->setItemData(
ui->cbScheme->currentIndex(),
mDefaultSchemeComboFont,
2021-06-20 22:54:16 +08:00
Qt::FontRole);
ui->cbScheme->setFont(mDefaultSchemeComboFont);
//ui->cbScheme->view()->setFont(mDefaultSchemeComboFont);
}
} catch (FileError e) {
QMessageBox::critical(this,tr("Error"),e.reason());
2021-06-20 22:54:16 +08:00
}
}
void EditorColorSchemeWidget::on_actionExport_Scheme_triggered()
{
QString filename = QFileDialog::getSaveFileName(this,
tr("Save"), QString(), tr("Color Scheme Files (*.scheme)"));
if (filename.isEmpty())
return;
try {
PColorScheme scheme = getCurrentScheme();
scheme->save(filename);
} catch (FileError e) {
QMessageBox::critical(this,tr("Error"),e.reason());
2021-06-20 22:54:16 +08:00
return;
}
}
void EditorColorSchemeWidget::on_actionDelete_Scheme_triggered()
{
QString name = ui->cbScheme->currentText();
if (QMessageBox::warning(this,tr("Confirm Delete Scheme"),
2021-06-20 22:54:16 +08:00
tr("Scheme '%1' will be deleted!<br />Do you really want to continue?")
.arg(name),
QMessageBox::Yes, QMessageBox::No)!=QMessageBox::Yes)
return;
try {
if (pColorManager->remove(name)) {
if (mModifiedSchemes.contains(name))
mModifiedSchemes.remove(name);
ui->cbScheme->removeItem(ui->cbScheme->currentIndex());
if (name == pSettings->editor().colorScheme())
doSave();
}
} catch (FileError e) {
QMessageBox::critical(this,tr("Error"),e.reason());
2021-06-20 22:54:16 +08:00
}
}
ColorSchemeItemDelegate::ColorSchemeItemDelegate(QObject *parent):
QStyledItemDelegate{parent}
{
}
void ColorSchemeItemDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
{
QStyledItemDelegate::initStyleOption(option,index);
QVariant value = index.data(Qt::FontRole);
if (value.isValid() && !value.isNull()) {
option->font = qvariant_cast<QFont>(value);
option->fontMetrics = QFontMetrics(option->font);
}
}