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-08 21:41:42 +08:00
|
|
|
#include "editorfontwidget.h"
|
2024-03-13 19:17:25 +08:00
|
|
|
#include "editor.h"
|
2021-06-08 21:41:42 +08:00
|
|
|
#include "ui_editorfontwidget.h"
|
2021-06-09 17:12:23 +08:00
|
|
|
#include "../settings.h"
|
|
|
|
#include "../mainwindow.h"
|
2024-03-13 19:17:25 +08:00
|
|
|
#include "../iconsmanager.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "utils/font.h"
|
2024-03-17 11:18:09 +08:00
|
|
|
#include "widgets/editorfontdialog.h"
|
|
|
|
#include <qabstractitemmodel.h>
|
2024-03-13 19:17:25 +08:00
|
|
|
|
2024-03-17 11:18:09 +08:00
|
|
|
Qt::ItemFlags EditorFontModel::flags(const QModelIndex &index) const
|
2024-03-13 19:17:25 +08:00
|
|
|
{
|
2024-03-17 11:18:09 +08:00
|
|
|
Qt::ItemFlags flags = Qt::NoItemFlags;
|
|
|
|
if (index.isValid()) {
|
|
|
|
flags = Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable ;
|
|
|
|
} else if (index.row() == -1) {
|
|
|
|
// -1 means it's a drop target?
|
|
|
|
flags = Qt::ItemIsDropEnabled;
|
2024-03-13 19:17:25 +08:00
|
|
|
}
|
2024-03-17 11:18:09 +08:00
|
|
|
return flags;
|
2024-03-13 19:17:25 +08:00
|
|
|
}
|
2021-06-08 21:41:42 +08:00
|
|
|
|
|
|
|
EditorFontWidget::EditorFontWidget(const QString& name, const QString& group, QWidget *parent) :
|
|
|
|
SettingsWidget(name,group,parent),
|
2024-03-17 11:18:09 +08:00
|
|
|
ui(new Ui::EditorFontWidget),
|
|
|
|
mModel(pSettings->editor().fontFamilies())
|
2021-06-08 21:41:42 +08:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2024-03-13 19:17:25 +08:00
|
|
|
|
|
|
|
QItemSelectionModel *m = ui->lstFontList->selectionModel();
|
|
|
|
ui->lstFontList->setModel(&mModel);
|
|
|
|
delete m;
|
2024-03-17 11:18:09 +08:00
|
|
|
ui->lstFontList->setDragEnabled(true);
|
|
|
|
ui->lstFontList->setAcceptDrops(true);
|
|
|
|
ui->lstFontList->setDropIndicatorShown(true);
|
|
|
|
ui->lstFontList->setDragDropMode(QAbstractItemView::InternalMove);
|
2021-06-08 21:41:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EditorFontWidget::~EditorFontWidget()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorFontWidget::on_chkGutterOnlyMonospacedFonts_stateChanged(int)
|
|
|
|
{
|
|
|
|
if (ui->chkGutterOnlyMonospacedFonts->isChecked()) {
|
|
|
|
ui->cbGutterFont->setFontFilters(QFontComboBox::FontFilter::MonospacedFonts);
|
|
|
|
} else {
|
|
|
|
ui->cbGutterFont->setFontFilters(QFontComboBox::FontFilter::AllFonts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 19:17:25 +08:00
|
|
|
void EditorFontWidget::on_btnAddFont_clicked()
|
|
|
|
{
|
2024-03-17 11:18:09 +08:00
|
|
|
QModelIndex index = ui->lstFontList->currentIndex();
|
|
|
|
int insertPos = index.isValid() ? index.row() + 1 : mModel.rowCount();
|
|
|
|
EditorFontDialog dlg(this);
|
|
|
|
if (dlg.exec() == QDialog::Accepted) {
|
|
|
|
mModel.insertRow(insertPos);
|
|
|
|
mModel.setData(mModel.index(insertPos), dlg.fontFamily());
|
|
|
|
ui->lstFontList->setCurrentIndex(mModel.index(insertPos));
|
|
|
|
}
|
2024-03-13 19:17:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorFontWidget::on_btnRemoveFont_clicked()
|
|
|
|
{
|
|
|
|
QModelIndex index = ui->lstFontList->currentIndex();
|
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
2024-03-17 11:18:09 +08:00
|
|
|
mModel.removeRow(index.row());
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorFontWidget::on_btnModifyFont_clicked()
|
|
|
|
{
|
|
|
|
QModelIndex index = ui->lstFontList->currentIndex();
|
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
|
|
|
EditorFontDialog dlg(this);
|
|
|
|
dlg.setFontFamily(mModel.data(index, Qt::DisplayRole).toString());
|
|
|
|
if (dlg.exec() == QDialog::Accepted) {
|
|
|
|
mModel.setData(index, dlg.fontFamily());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorFontWidget::on_btnResetFonts_clicked()
|
|
|
|
{
|
|
|
|
mModel.setStringList(defaultEditorFonts());
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorFontWidget::on_btnMoveFontToTop_clicked()
|
|
|
|
{
|
|
|
|
QModelIndex index = ui->lstFontList->currentIndex();
|
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
|
|
|
if (index.row() == 0)
|
|
|
|
return;
|
|
|
|
mModel.moveRow(QModelIndex(), index.row(), QModelIndex(), 0);
|
2024-03-13 19:17:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorFontWidget::on_btnMoveFontUp_clicked()
|
|
|
|
{
|
|
|
|
QModelIndex index = ui->lstFontList->currentIndex();
|
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
2024-03-17 11:18:09 +08:00
|
|
|
if (index.row() == 0)
|
|
|
|
return;
|
|
|
|
mModel.moveRow(QModelIndex(), index.row(), QModelIndex(), index.row() - 1);
|
2024-03-13 19:17:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorFontWidget::on_btnMoveFontDown_clicked()
|
|
|
|
{
|
|
|
|
QModelIndex index = ui->lstFontList->currentIndex();
|
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
2024-03-17 11:18:09 +08:00
|
|
|
if (index.row() == mModel.rowCount() - 1)
|
|
|
|
return;
|
|
|
|
mModel.moveRow(QModelIndex(), index.row(), QModelIndex(), index.row() + 2);
|
2024-03-13 19:17:25 +08:00
|
|
|
}
|
|
|
|
|
2024-03-17 11:18:09 +08:00
|
|
|
void EditorFontWidget::on_btnMoveFontToBottom_clicked()
|
2024-03-13 19:17:25 +08:00
|
|
|
{
|
2024-03-17 11:18:09 +08:00
|
|
|
QModelIndex index = ui->lstFontList->currentIndex();
|
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
|
|
|
if (index.row() == mModel.rowCount() - 1)
|
|
|
|
return;
|
|
|
|
mModel.moveRow(QModelIndex(), index.row(), QModelIndex(), mModel.rowCount());
|
2024-03-13 19:17:25 +08:00
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
void EditorFontWidget::doLoad()
|
|
|
|
{
|
2021-06-09 17:12:23 +08:00
|
|
|
//pSettings->editor().load();
|
|
|
|
//font
|
|
|
|
ui->spinFontSize->setValue(pSettings->editor().fontSize());
|
2022-12-20 20:38:02 +08:00
|
|
|
ui->spinLineSpacing->setValue(pSettings->editor().lineSpacing());
|
2022-01-10 20:16:26 +08:00
|
|
|
ui->chkLigature->setChecked(pSettings->editor().enableLigaturesSupport());
|
2024-02-28 11:51:02 +08:00
|
|
|
ui->chkForceFixedFontWidth->setChecked(pSettings->editor().forceFixedFontWidth());
|
2022-12-20 20:38:02 +08:00
|
|
|
ui->chkLeadingSpaces->setChecked(pSettings->editor().showLeadingSpaces());
|
|
|
|
ui->chkInnerSpaces->setChecked(pSettings->editor().showInnerSpaces());
|
|
|
|
ui->chkTrailingSpaces->setChecked(pSettings->editor().showTrailingSpaces());
|
|
|
|
ui->chkLineBreaks->setChecked(pSettings->editor().showLineBreaks());
|
2021-06-09 17:12:23 +08:00
|
|
|
//gutter
|
|
|
|
ui->chkGutterVisible->setChecked(pSettings->editor().gutterVisible());
|
|
|
|
ui->chkAutoSizeGutter->setChecked(pSettings->editor().gutterAutoSize());
|
|
|
|
ui->spinGutterLeftOffset->setValue(pSettings->editor().gutterLeftOffset());
|
|
|
|
ui->spinGutterRightOffset->setValue(pSettings->editor().gutterRightOffset());
|
|
|
|
ui->spinGutterDigitsCount->setValue(pSettings->editor().gutterDigitsCount());
|
|
|
|
ui->grpGutterShowLineNumbers->setChecked(pSettings->editor().gutterShowLineNumbers());
|
|
|
|
ui->chkAddLeadingZeros->setChecked(pSettings->editor().gutterAddLeadingZero());
|
|
|
|
ui->chkLineNumbersStartsZero->setChecked(pSettings->editor().gutterLineNumbersStartZero());
|
|
|
|
ui->grpUseCustomFont->setChecked(pSettings->editor().gutterUseCustomFont());
|
|
|
|
ui->chkGutterOnlyMonospacedFonts->setChecked(pSettings->editor().gutterFontOnlyMonospaced());
|
|
|
|
ui->cbGutterFont->setCurrentFont(QFont(pSettings->editor().gutterFontName()));
|
|
|
|
ui->spinGutterFontSize->setValue(pSettings->editor().gutterFontSize());
|
2021-06-08 21:41:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorFontWidget::doSave()
|
|
|
|
{
|
2021-06-09 17:12:23 +08:00
|
|
|
//font
|
2024-03-17 11:18:09 +08:00
|
|
|
pSettings->editor().setFontFamilies(mModel.stringList());
|
2021-06-09 17:12:23 +08:00
|
|
|
pSettings->editor().setFontSize(ui->spinFontSize->value());
|
2022-12-20 20:38:02 +08:00
|
|
|
pSettings->editor().setLineSpacing(ui->spinLineSpacing->value());
|
|
|
|
|
2022-01-10 20:16:26 +08:00
|
|
|
pSettings->editor().setEnableLigaturesSupport(ui->chkLigature->isChecked());
|
2024-02-28 11:51:02 +08:00
|
|
|
pSettings->editor().setForceFixedFontWidth(ui->chkForceFixedFontWidth->isChecked());
|
2022-12-20 20:38:02 +08:00
|
|
|
pSettings->editor().setShowLeadingSpaces(ui->chkLeadingSpaces->isChecked());
|
|
|
|
pSettings->editor().setShowInnerSpaces(ui->chkInnerSpaces->isChecked());
|
|
|
|
pSettings->editor().setShowTrailingSpaces(ui->chkTrailingSpaces->isChecked());
|
|
|
|
pSettings->editor().setShowLineBreaks(ui->chkLineBreaks->isChecked());
|
2021-06-09 17:12:23 +08:00
|
|
|
//gutter
|
|
|
|
pSettings->editor().setGutterVisible(ui->chkGutterVisible->isChecked());
|
|
|
|
pSettings->editor().setGutterAutoSize(ui->chkAutoSizeGutter->isChecked());
|
|
|
|
pSettings->editor().setGutterLeftOffset(ui->spinGutterLeftOffset->value());
|
|
|
|
pSettings->editor().setGutterRightOffset(ui->spinGutterRightOffset->value());
|
|
|
|
pSettings->editor().setGutterDigitsCount(ui->spinGutterDigitsCount->value());
|
|
|
|
pSettings->editor().setGutterShowLineNumbers(ui->grpGutterShowLineNumbers->isChecked());
|
|
|
|
pSettings->editor().setGutterAddLeadingZero(ui->chkAddLeadingZeros->isChecked());
|
|
|
|
pSettings->editor().setGutterLineNumbersStartZero(ui->chkLineNumbersStartsZero->isChecked());
|
|
|
|
pSettings->editor().setGutterUseCustomFont(ui->grpUseCustomFont->isChecked());
|
|
|
|
pSettings->editor().setGutterFontOnlyMonospaced(ui->chkGutterOnlyMonospacedFonts->isChecked());
|
|
|
|
pSettings->editor().setGutterFontName(ui->cbGutterFont->currentFont().family());
|
|
|
|
pSettings->editor().setGutterFontSize(ui->spinGutterFontSize->value());
|
|
|
|
|
|
|
|
pSettings->editor().save();
|
2024-02-29 19:17:14 +08:00
|
|
|
QFont::cleanup();
|
2021-06-09 17:12:23 +08:00
|
|
|
pMainWindow->updateEditorSettings();
|
2021-06-08 21:41:42 +08:00
|
|
|
}
|
2024-02-27 13:05:38 +08:00
|
|
|
|
2024-03-13 19:17:25 +08:00
|
|
|
void EditorFontWidget::updateIcons(const QSize &/*size*/) {
|
|
|
|
pIconsManager->setIcon(ui->btnAddFont, IconsManager::ACTION_MISC_ADD);
|
|
|
|
pIconsManager->setIcon(ui->btnRemoveFont, IconsManager::ACTION_MISC_REMOVE);
|
2024-03-17 11:18:09 +08:00
|
|
|
pIconsManager->setIcon(ui->btnModifyFont, IconsManager::ACTION_MISC_RENAME);
|
|
|
|
pIconsManager->setIcon(ui->btnResetFonts, IconsManager::ACTION_MISC_RESET);
|
|
|
|
pIconsManager->setIcon(ui->btnMoveFontToTop, IconsManager::ACTION_MISC_MOVETOP);
|
2024-03-13 19:17:25 +08:00
|
|
|
pIconsManager->setIcon(ui->btnMoveFontUp, IconsManager::ACTION_MISC_MOVEUP);
|
|
|
|
pIconsManager->setIcon(ui->btnMoveFontDown, IconsManager::ACTION_MISC_MOVEDOWN);
|
2024-03-17 11:18:09 +08:00
|
|
|
pIconsManager->setIcon(ui->btnMoveFontToBottom, IconsManager::ACTION_MISC_MOVEBOTTOM);
|
2024-02-27 13:05:38 +08:00
|
|
|
}
|