custom foreground and background color
This commit is contained in:
parent
337a05720b
commit
43e319cff1
2
main.cpp
2
main.cpp
|
@ -4,10 +4,10 @@
|
|||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
qSetMessagePattern("[ %{file}: %{line} ] %{message}");
|
||||
QApplication app(argc, argv);
|
||||
app.setOrganizationName("HTY");
|
||||
app.setApplicationName("HTYEdit");
|
||||
qSetMessagePattern("[ %{file}: %{line} ] %{message}");
|
||||
MainWindow w;
|
||||
w.show();
|
||||
return app.exec();
|
||||
|
|
112
mainwindow.cpp
112
mainwindow.cpp
|
@ -1,6 +1,7 @@
|
|||
#include "mainwindow.h"
|
||||
#include "ui_mainwindow.h"
|
||||
#include "mdichild.h"
|
||||
#include "highlighter.h"
|
||||
#include <QDesktopWidget>
|
||||
#include <QMessageBox>
|
||||
#include <QTextEdit>
|
||||
|
@ -23,6 +24,8 @@
|
|||
#include <QTextBlock>
|
||||
#include <QComboBox>
|
||||
#include <QTextDocumentFragment>
|
||||
#include <QListWidget>
|
||||
#include <QColorDialog>
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent) :
|
||||
QMainWindow(parent),
|
||||
|
@ -59,7 +62,7 @@ MainWindow::MainWindow(QWidget *parent) :
|
|||
ui->statusBar->addWidget(LS1);
|
||||
ui->statusBar->addWidget(LS2);
|
||||
ui->statusBar->addWidget(LS3);
|
||||
connect(ui->action_quit, SIGNAL(triggered()), qApp, SLOT(quit()));
|
||||
connect(ui->action_quit, SIGNAL(triggered()), qApp, SLOT(quit()));
|
||||
connect(ui->mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(subWindowActivate(QMdiSubWindow*)));
|
||||
connect(ui->action_window_output, &QAction::toggled, this, [=](bool b){
|
||||
if (b) {
|
||||
|
@ -79,15 +82,10 @@ MainWindow::MainWindow(QWidget *parent) :
|
|||
qDebug() << args;
|
||||
QDateTime currentDateTime = QDateTime::currentDateTime();
|
||||
QString log = currentDateTime.toString("yyyy/MM/dd HH:mm:ss") + " " + args.at(0);
|
||||
if(args.length()>1){
|
||||
for(int i=1; i<args.length(); i++){
|
||||
if (args.length() > 1) {
|
||||
for (int i=1; i<args.length(); i++) {
|
||||
log += " " + args.at(i);
|
||||
if(args.at(i).startsWith("file://")){
|
||||
QUrl url(args.at(i));
|
||||
open(url.toLocalFile());
|
||||
}else{
|
||||
open(args.at(i));
|
||||
}
|
||||
open(args.at(i));
|
||||
}
|
||||
}
|
||||
log += "\n";
|
||||
|
@ -107,6 +105,74 @@ MainWindow::~MainWindow()
|
|||
delete ui;
|
||||
}
|
||||
|
||||
void MainWindow::on_action_theme_triggered()
|
||||
{
|
||||
QDialog *dialog = new QDialog(this);
|
||||
dialog->setWindowTitle("主题");
|
||||
dialog->setFixedSize(400, 300);
|
||||
QGridLayout *gridLayout = new QGridLayout;
|
||||
QLabel *label = new QLabel("对象");
|
||||
gridLayout->addWidget(label,0,0,1,1,Qt::AlignCenter);
|
||||
label = new QLabel("前景色");
|
||||
gridLayout->addWidget(label,0,1,1,1,Qt::AlignCenter);
|
||||
label = new QLabel("背景色");
|
||||
gridLayout->addWidget(label,0,2,1,1,Qt::AlignCenter);
|
||||
QListWidget *listWidget = new QListWidget(dialog);
|
||||
QListWidgetItem *LWI = new QListWidgetItem("通用");
|
||||
listWidget->addItem(LWI);
|
||||
gridLayout->addWidget(listWidget,1,0,1,1);
|
||||
QPushButton *pushButton_color_text = new QPushButton(dialog);
|
||||
QColor color = intToColor(settings.value("ColorTextGeneral", 16777215).toInt());
|
||||
QPalette plt = pushButton_color_text->palette();
|
||||
plt.setColor(QPalette::Button, color);
|
||||
pushButton_color_text->setPalette(plt);
|
||||
connect(pushButton_color_text, &QPushButton::pressed, [=](){
|
||||
QPalette plt = pushButton_color_text->palette();
|
||||
QColor color = QColorDialog::getColor(plt.color(QPalette::Button), dialog);
|
||||
if (color.isValid()) {
|
||||
plt.setColor(QPalette::Button, color);
|
||||
pushButton_color_text->setPalette(plt);
|
||||
QMdiSubWindow *window = ui->mdiArea->currentSubWindow();
|
||||
if (window != nullptr) {
|
||||
MdiChild *child = (MdiChild*)(window->widget());
|
||||
QPalette plt = child->palette();
|
||||
plt.setColor(QPalette::Text, color);
|
||||
child->setPalette(plt);
|
||||
Highlighter *highlighter = new Highlighter(child->document());
|
||||
Q_UNUSED(highlighter);
|
||||
}
|
||||
settings.setValue("ColorTextGeneral", color.blue() << 16 | color.green() << 8 | color.red());
|
||||
}
|
||||
});
|
||||
gridLayout->addWidget(pushButton_color_text,1,1,1,1);
|
||||
QPushButton *pushButton_color_base = new QPushButton(dialog);
|
||||
color = intToColor(settings.value("ColorBaseGeneral", 0).toInt());
|
||||
plt = pushButton_color_base->palette();
|
||||
plt.setColor(QPalette::Button, color);
|
||||
pushButton_color_base->setPalette(plt);
|
||||
connect(pushButton_color_base, &QPushButton::pressed, [=](){
|
||||
QPalette plt = pushButton_color_base->palette();
|
||||
QColor color = QColorDialog::getColor(plt.color(QPalette::Button), dialog);
|
||||
if (color.isValid()) {
|
||||
plt.setColor(QPalette::Button, color);
|
||||
pushButton_color_base->setPalette(plt);
|
||||
QMdiSubWindow *window = ui->mdiArea->currentSubWindow();
|
||||
if (window != nullptr) {
|
||||
MdiChild *child = (MdiChild*)(window->widget());
|
||||
QPalette plt = child->palette();
|
||||
plt.setColor(QPalette::Base, color);
|
||||
child->setPalette(plt);
|
||||
Highlighter *highlighter = new Highlighter(child->document());
|
||||
Q_UNUSED(highlighter);
|
||||
}
|
||||
settings.setValue("ColorBaseGeneral", color.blue() << 16 | color.green() << 8 | color.red());
|
||||
}
|
||||
});
|
||||
gridLayout->addWidget(pushButton_color_base,1,2,1,1);
|
||||
dialog->setLayout(gridLayout);
|
||||
dialog->show();
|
||||
}
|
||||
|
||||
void MainWindow::on_action_aboutQt_triggered()
|
||||
{
|
||||
QMessageBox::aboutQt(nullptr, "关于 Qt");
|
||||
|
@ -389,7 +455,7 @@ void MainWindow::on_action_zoomin_triggered()
|
|||
void MainWindow::on_action_zoomout_triggered()
|
||||
{
|
||||
QMdiSubWindow *window = ui->mdiArea->currentSubWindow();
|
||||
if(window != nullptr){
|
||||
if (window != nullptr) {
|
||||
((QTextEdit*)(window->widget()))->zoomOut();
|
||||
}
|
||||
}
|
||||
|
@ -397,7 +463,7 @@ void MainWindow::on_action_zoomout_triggered()
|
|||
void MainWindow::on_action_undo_triggered()
|
||||
{
|
||||
QMdiSubWindow *window = ui->mdiArea->currentSubWindow();
|
||||
if(window != nullptr){
|
||||
if (window != nullptr) {
|
||||
((QTextEdit*)(window->widget()))->undo();
|
||||
}
|
||||
}
|
||||
|
@ -405,7 +471,7 @@ void MainWindow::on_action_undo_triggered()
|
|||
void MainWindow::on_action_redo_triggered()
|
||||
{
|
||||
QMdiSubWindow *window = ui->mdiArea->currentSubWindow();
|
||||
if(window != nullptr){
|
||||
if (window != nullptr) {
|
||||
((QTextEdit*)(window->widget()))->redo();
|
||||
}
|
||||
}
|
||||
|
@ -430,7 +496,7 @@ void MainWindow::find()
|
|||
MB.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
|
||||
MB.setButtonText(QMessageBox::Yes,QString("是"));
|
||||
MB.setButtonText(QMessageBox::No,QString("否"));
|
||||
if(MB.exec() == QMessageBox::Yes){
|
||||
if (MB.exec() == QMessageBox::Yes) {
|
||||
QTextCursor cursor = ((QTextEdit*)(window->widget()))->textCursor();
|
||||
cursor.setPosition(0, QTextCursor::MoveAnchor);
|
||||
((QTextEdit*)(window->widget()))->setTextCursor(cursor);
|
||||
|
@ -505,7 +571,7 @@ void MainWindow::on_action_font_triggered()
|
|||
font = QFont(SLFont.at(0),SLFont.at(1).toInt(),SLFont.at(2).toInt(),SLFont.at(3).toInt());
|
||||
}
|
||||
font = QFontDialog::getFont(&ok, font, this, "选择字体");
|
||||
if(ok){
|
||||
if (ok) {
|
||||
((QTextEdit*)(window->widget()))->setCurrentFont(font);
|
||||
QString sfont = font.family() + "," + QString::number(font.pointSize()) + "," + QString::number(font.weight()) + "," + font.italic();
|
||||
settings.setValue("Font", sfont);
|
||||
|
@ -523,29 +589,29 @@ void MainWindow::SyntaxHighlight()
|
|||
//HTML
|
||||
QString str="!DOCTYPE,html,link,head,meta,body,title,style,script,p,br,pre,table,tr,td,input,div,img,a,h1,h2,h3,h4,h6,select,option,ul,ol,li,canvas,fieldset,legend,input,button";
|
||||
QStringList list=str.split(",");
|
||||
for(int i=0;i<list.size();i++){
|
||||
for (int i=0;i<list.size();i++) {
|
||||
//cursor=((QTextEdit*)(window->widget()))->textCursor();
|
||||
cursor.setPosition(0,QTextCursor::MoveAnchor);
|
||||
((QTextEdit*)(window->widget()))->setTextCursor(cursor);
|
||||
while(((QTextEdit*)(window->widget()))->find("<" + list[i] + ">")){
|
||||
while (((QTextEdit*)(window->widget()))->find("<" + list[i] + ">")) {
|
||||
((QTextEdit*)(window->widget()))->setTextColor(QColor(255,0,0));
|
||||
}
|
||||
//cursor=((QTextEdit*)(window->widget()))->textCursor();
|
||||
cursor.setPosition(0,QTextCursor::MoveAnchor);
|
||||
((QTextEdit*)(window->widget()))->setTextCursor(cursor);
|
||||
while(((QTextEdit*)(window->widget()))->find("<" + list[i])){
|
||||
while (((QTextEdit*)(window->widget()))->find("<" + list[i])) {
|
||||
((QTextEdit*)(window->widget()))->setTextColor(QColor(255,0,0));
|
||||
}
|
||||
//cursor=((QTextEdit*)(window->widget()))->textCursor();
|
||||
cursor.setPosition(0,QTextCursor::MoveAnchor);
|
||||
((QTextEdit*)(window->widget()))->setTextCursor(cursor);
|
||||
while(((QTextEdit*)(window->widget()))->find("</" + list[i] + ">")){
|
||||
while (((QTextEdit*)(window->widget()))->find("</" + list[i] + ">")) {
|
||||
((QTextEdit*)(window->widget()))->setTextColor(QColor(255,0,0));
|
||||
}
|
||||
//cursor=((QTextEdit*)(window->widget()))->textCursor();
|
||||
cursor.setPosition(0,QTextCursor::MoveAnchor);
|
||||
((QTextEdit*)(window->widget()))->setTextCursor(cursor);
|
||||
while(((QTextEdit*)(window->widget()))->find(">")){
|
||||
while (((QTextEdit*)(window->widget()))->find(">")) {
|
||||
((QTextEdit*)(window->widget()))->setTextColor(QColor(255,0,0));
|
||||
}
|
||||
}
|
||||
|
@ -823,4 +889,12 @@ void MainWindow::comboBoxHChanged(QString s)
|
|||
MdiChild *child = (MdiChild*)(window->widget());
|
||||
child->insertH(s);
|
||||
}
|
||||
}
|
||||
|
||||
QColor MainWindow::intToColor(int intColor)
|
||||
{
|
||||
int red = intColor & 255;
|
||||
int green = intColor >> 8 & 255;
|
||||
int blue = intColor >> 16 & 255;
|
||||
return QColor(red, green, blue);
|
||||
}
|
|
@ -32,6 +32,7 @@ private:
|
|||
QSettings settings;
|
||||
void open(QString fileName);
|
||||
void readSettings();
|
||||
QColor intToColor(int intColor);
|
||||
|
||||
protected:
|
||||
void dragEnterEvent(QDragEnterEvent *event);
|
||||
|
@ -46,6 +47,7 @@ private slots:
|
|||
void on_action_saveas_triggered();
|
||||
void on_action_print_triggered();
|
||||
void on_action_printPreview_triggered();
|
||||
void on_action_theme_triggered();
|
||||
void on_action_changelog_triggered();
|
||||
void on_action_aboutQt_triggered();
|
||||
void on_action_about_triggered();
|
||||
|
|
|
@ -74,7 +74,7 @@
|
|||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>800</width>
|
||||
<height>36</height>
|
||||
<height>27</height>
|
||||
</rect>
|
||||
</property>
|
||||
<widget class="QMenu" name="menu">
|
||||
|
@ -90,7 +90,7 @@
|
|||
<addaction name="action_printPreview"/>
|
||||
<addaction name="action_quit"/>
|
||||
</widget>
|
||||
<widget class="QMenu" name="menu_2">
|
||||
<widget class="QMenu" name="menu_edit">
|
||||
<property name="title">
|
||||
<string>编辑</string>
|
||||
</property>
|
||||
|
@ -103,7 +103,7 @@
|
|||
<addaction name="action_replace"/>
|
||||
<addaction name="action_indent"/>
|
||||
</widget>
|
||||
<widget class="QMenu" name="menu_3">
|
||||
<widget class="QMenu" name="menu_help">
|
||||
<property name="title">
|
||||
<string>帮助</string>
|
||||
</property>
|
||||
|
@ -128,11 +128,18 @@
|
|||
<addaction name="action_zoomin"/>
|
||||
<addaction name="action_zoomout"/>
|
||||
</widget>
|
||||
<widget class="QMenu" name="menu_set">
|
||||
<property name="title">
|
||||
<string>设置</string>
|
||||
</property>
|
||||
<addaction name="action_theme"/>
|
||||
</widget>
|
||||
<addaction name="menu"/>
|
||||
<addaction name="menu_2"/>
|
||||
<addaction name="menu_edit"/>
|
||||
<addaction name="menu_5"/>
|
||||
<addaction name="menu_window"/>
|
||||
<addaction name="menu_3"/>
|
||||
<addaction name="menu_set"/>
|
||||
<addaction name="menu_help"/>
|
||||
</widget>
|
||||
<widget class="QToolBar" name="mainToolBar">
|
||||
<property name="movable">
|
||||
|
@ -539,10 +546,7 @@
|
|||
<string>去除Tag</string>
|
||||
</property>
|
||||
<property name="font">
|
||||
<font>
|
||||
<underline>false</underline>
|
||||
<strikeout>false</strikeout>
|
||||
</font>
|
||||
<font/>
|
||||
</property>
|
||||
</action>
|
||||
<action name="action_deleteBR">
|
||||
|
@ -553,6 +557,11 @@
|
|||
<string>删除换行</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="action_theme">
|
||||
<property name="text">
|
||||
<string>主题</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<layoutdefault spacing="6" margin="11"/>
|
||||
<resources>
|
||||
|
|
18
mdichild.cpp
18
mdichild.cpp
|
@ -53,10 +53,12 @@ bool MdiChild::loadFile(QString filename)
|
|||
QFile *file = new QFile;
|
||||
file->setFileName(filename);
|
||||
bool ok = file->open(QIODevice::ReadOnly);
|
||||
if(ok){
|
||||
if (ok) {
|
||||
QPalette plt = palette();
|
||||
plt.setColor(QPalette::Text,QColor(Qt::white));
|
||||
plt.setBrush(QPalette::Base,QBrush(Qt::black));
|
||||
QColor color = intToColor(settings.value("ColorTextGeneral", 0).toInt());
|
||||
plt.setColor(QPalette::Text, color);
|
||||
color = intToColor(settings.value("ColorBaseGeneral", 0).toInt());
|
||||
plt.setBrush(QPalette::Base, color);
|
||||
setPalette(plt);
|
||||
QTextStream TS(file);
|
||||
// 还是乱码
|
||||
|
@ -71,7 +73,7 @@ bool MdiChild::loadFile(QString filename)
|
|||
showMaximized();
|
||||
zoomIn(4);
|
||||
return true;
|
||||
}else{
|
||||
} else {
|
||||
QMessageBox::warning(this,"错误", QString(" %1:\n%2").arg(path).arg(file->errorString()));
|
||||
return false;
|
||||
}
|
||||
|
@ -371,4 +373,12 @@ void MdiChild::deleteBR()
|
|||
//s = s.replace(">\\s+<", "><"); //无效
|
||||
s = s.simplified(); //去首尾空格,中间多个空格替换为一个空格
|
||||
textCursor().insertText(s);
|
||||
}
|
||||
|
||||
QColor MdiChild::intToColor(int intColor)
|
||||
{
|
||||
int red = intColor & 255;
|
||||
int green = intColor >> 8 & 255;
|
||||
int blue = intColor >> 16 & 255;
|
||||
return QColor(red, green, blue);
|
||||
}
|
|
@ -9,6 +9,7 @@ class LineNumberArea;
|
|||
class MdiChild : public QPlainTextEdit
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
MdiChild(QWidget *parent);
|
||||
bool loadFile(QString);
|
||||
|
@ -31,6 +32,7 @@ private:
|
|||
QSettings settings;
|
||||
LineNumberArea *lineNumberArea;
|
||||
QFont font;
|
||||
QColor intToColor(int intColor);
|
||||
|
||||
protected:
|
||||
void wheelEvent(QWheelEvent *e);
|
||||
|
|
Loading…
Reference in New Issue