-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainwindow.cpp
156 lines (134 loc) · 5.11 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "mainwindow.h"
#include <QWidget>
#include <QList>
#include <QDebug>
#include <QFile>
#include <QXmlStreamWriter>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){
this->parentW=parent;
this->setAttribute(Qt::WA_DeleteOnClose);
this->setWindowFlags(Qt::Window|Qt::FramelessWindowHint);
this->setCentralWidget(new QWidget());
initTitlebar();
styleOptionBar=new StyleOptionBar(this);
content=new QTextEdit(this);
statusBar=new QStatusBar(this);
this->setFocusProxy(this->content);
connect(styleOptionBar,SIGNAL(styleBtnClickedSignal(QString)),this,SLOT(onStyleBtnClicked(QString)));
styleOptionBar->setVisible(false);
content->setFocusPolicy(Qt::StrongFocus);
content->setStyleSheet("background-color:transparent;"
"font-size:14px;"
"border:none;"
"font-family:'Microsoft Yahei'");
statusBar->setFixedHeight(8);
statusBar->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
QVBoxLayout *main_layout=new QVBoxLayout(this);
main_layout->addWidget(titlebar);
main_layout->addWidget(styleOptionBar);
content_layout=new QVBoxLayout(this);
content_layout->addWidget(content);
content_layout->setContentsMargins(12,0,12,4);
main_layout->addLayout(content_layout);
main_layout->addWidget(statusBar);
main_layout->setContentsMargins(0,0,0,0);
centralWidget()->setLayout(main_layout);
}
MainWindow::~MainWindow(){
delete content;
delete styleOptionBar;
delete titlebar;
}
QString MainWindow::getContentText(){
return content->toPlainText();
}
void MainWindow::setContentTest(QString text){
this->content->append(text);
}
void MainWindow::initTitlebar(){
titlebar=new TitleBar(this);
titlebar->setBtnsVisible(false);
connect(titlebar,SIGNAL(newBtnClickedSignal()),this,SLOT(onNewBtnClicked()));
connect(titlebar,SIGNAL(settingBtnClickedSignal()),this,SLOT(onSettingBtnClicked()));
connect(titlebar,SIGNAL(deleteBtnClickedSignal()),this,SLOT(onDeleteBtnClicked()));
connect(titlebar,SIGNAL(closeBtnClickedSignal()),this,SLOT(onCloseBtnClicked()));
}
void MainWindow::onNewBtnClicked(){
MainWindow *newWindow=new MainWindow(this->parentW);
newWindow->setGeometry(this->pos().x()+20,
this->pos().y()+20,
this->size().width(),
this->size().height());
newWindow->setStyleSheet(this->style);
newWindow->show();
newWindow->setFocus();
}
void MainWindow::onSettingBtnClicked(){
this->styleOptionBar->setVisible(!styleOptionBar->isVisible());
this->titlebar->setVisible(false);
this->styleOptionBar->setFocus();
//this->content_layout->setContentsMargins(12,0,12,4);
}
void MainWindow::onDeleteBtnClicked(){
this->hide();
bool isAllDeleted=true;
QList<MainWindow *> windows= this->parentWidget()->findChildren<MainWindow *>();
if(!windows.isEmpty()){
foreach (MainWindow *mainwindow, windows) {
if(mainwindow->isVisible()){
isAllDeleted=false;
break;
}
}
}
if(isAllDeleted)
this->onCloseBtnClicked();
}
void MainWindow::onCloseBtnClicked(){
this->close();
}
void MainWindow::onStyleBtnClicked(QString btnName){
this->style=btnName;
this->setStyleSheet(btnName);
this->styleOptionBar->setVisible(false);
this->titlebar->setVisible(true);
this->content->setFocus();
}
void MainWindow::closeEvent(QCloseEvent *event){
QList<MainWindow *> windows= this->parentWidget()->findChildren<MainWindow *>();
if(!windows.isEmpty()){
QFile file("record.xml");
file.open(QIODevice::WriteOnly);
QXmlStreamWriter xmlWriter(&file);
xmlWriter.setAutoFormatting(true);
xmlWriter.writeStartDocument();
xmlWriter.writeStartElement("windows");
foreach (MainWindow *mainwindow, windows) {
if(mainwindow->isVisible()){
xmlWriter.writeStartElement("window");
xmlWriter.writeTextElement("x",QString::number(mainwindow->pos().x()));
xmlWriter.writeTextElement("y",QString::number(mainwindow->pos().y()));
xmlWriter.writeTextElement("width",QString::number(mainwindow->size().width()));
xmlWriter.writeTextElement("height",QString::number(mainwindow->size().height()));
xmlWriter.writeTextElement("style",mainwindow->style);
xmlWriter.writeTextElement("content",mainwindow->getContentText());
xmlWriter.writeEndElement();
}
}
xmlWriter.writeEndElement();
xmlWriter.writeEndDocument();
file.close();
}
return QWidget::closeEvent(event);
}
void MainWindow::enterEvent(QEvent *event){
this->titlebar->setBtnsVisible(true);
this->content->setFocus();
return QWidget::enterEvent(event);
}
void MainWindow::leaveEvent(QEvent *event){
this->titlebar->setBtnsVisible(false);
this->styleOptionBar->setVisible(false);
this->content->clearFocus();
return QWidget::leaveEvent(event);
}