-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.cpp
More file actions
255 lines (234 loc) · 7.34 KB
/
file.cpp
File metadata and controls
255 lines (234 loc) · 7.34 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//=============================================================
//
// 「ファイル」メニュー系
//
//=============================================================
#include "mainwindow.h"
#include "aboutdialog.h"
#include "global.h"
#include <QColor>
#include <QBrush>
#include <QFile>
#include <QTextStream>
#include <QTextEdit>
#include <QFileDialog>
#include <QMessageBox>
// 初期化
void MainWindow::initFile()
{
}
// ソースファイルが入ったフォルダを作る(リストビューに表示)
QTreeWidgetItem* MainWindow::createSrcFolder(QStringList files)
{
QTreeWidgetItem* folder = new QTreeWidgetItem(QStringList("src"));
foreach (const QString& file, files)
{
QString fileName = QFileInfo(file).fileName();
QTreeWidgetItem* item = new QTreeWidgetItem(QStringList(fileName));
item->setWhatsThis(0, file);
folder->addChild(item);
}
return folder;
}
// ソースファイルが入ったフォルダを作る(リストビューに表示)
QTreeWidgetItem* MainWindow::createAsmFolder(QString srcPath)
{
QTreeWidgetItem* folder = new QTreeWidgetItem(QStringList("asm"));
QString base = QFileInfo(srcPath).completeBaseName();
QString file = QFileInfo(srcPath).canonicalPath() + "/" + base + ".s";
if (QFileInfo(file).exists())
{
QString fileName = QFileInfo(file).fileName();
QTreeWidgetItem* item = new QTreeWidgetItem(QStringList(fileName));
item->setWhatsThis(0, file);
folder->addChild(item);
}
return folder;
}
// プロジェクトを作ってリストビューに表示
void MainWindow::createProject(QString filepath)
{
QFileInfo fileInfo = QFileInfo(filepath);
QString fileName = fileInfo.fileName();
QString projectName = QString(fileName).remove(QRegExp("[.].*"));
// リストビューにプロジェクトを追加
QTreeWidgetItem* item = new QTreeWidgetItem(QStringList(projectName));
item->setWhatsThis(0, fileInfo.canonicalFilePath());
item->addChild(createSrcFolder(QStringList(fileInfo.canonicalFilePath())));
item->addChild(createAsmFolder(fileInfo.canonicalFilePath()));
ui->treeWidget->addTopLevelItem(item);
ui->treeWidget->expandAll();
if (startupProject == NULL)
{
setStartupProject(item);
}
}
void MainWindow::createProject()
{
QStringList names = QFileDialog::getOpenFileNames(
this,
"select ML/Assembly files",
".",
"ML file (*.ml);;Assembly file (*.s)");
foreach (const QString& filepath, names)
{
createProject(filepath);
}
}
// プロジェクトを新規作成
void MainWindow::newProject()
{
QString filepath = QFileDialog::getSaveFileName(
this,
"set ML/Assembly file name",
".",
"ML/Assembly file (*.ml *.s)");
if (filepath == "") return;
// 空のファイルを生成
createNewFile(filepath);
// プロジェクトを開く
createProject(filepath);
}
// タブを新しく開いてファイル内容を表示
void MainWindow::createTextTab(const QString& path, const QString& tabName) {
for (int i = 0; i < ui->tabWidget->count(); i++)
{
if (path == ui->tabWidget->tabWhatsThis(i))
{
ui->tabWidget->setCurrentIndex(i);
return;
}
}
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::critical(this, "can't open file", "can't open " + path, QMessageBox::Ok, QMessageBox::Cancel);
return;
}
QTextStream in(&file);
QString text;
while (!in.atEnd())
{
text += in.readLine() + "\n";
}
// TODO : これのDELETE
QTextEdit* textEdit = new QTextEdit(this);
textEdit->setPlainText(text);
textEdit->setFont(QFont("Monospace", 12));
textEdit->setTabStopWidth(30);
ui->tabWidget->addTab(textEdit, tabName);
ui->tabWidget->setTabWhatsThis(ui->tabWidget->count() - 1, path);
ui->tabWidget->setCurrentIndex(ui->tabWidget->count() - 1);
// テキストが書き換わったらタブのタイトルを返る
connect(textEdit, SIGNAL(textChanged()), this, SLOT(changeTabTitle()));
}
void MainWindow::changeTabTitle()
{
int cur = ui->tabWidget->currentIndex();
QString title = ui->tabWidget->tabText(cur);
title = title.remove('*');
ui->tabWidget->setTabText(cur, "*" + title);
}
// ファイルをエディタ画面に開く
void MainWindow::openFile()
{
openItem(ui->treeWidget->currentItem(), 0);
}
// フォルダなら展開し、ファイルならエディタ画面に開く
void MainWindow::openItem(QTreeWidgetItem* item, int idx)
{
// 子ノードがあるならフォルダなので展開・畳み込みを行う
if (item->childCount() > 0)
{
if (item->isExpanded())
{
ui->treeWidget->expandItem(item);
}
else
{
ui->treeWidget->collapseItem(item);
}
return;
}
// ファイルが存在すればエディタ画面に開く
QString filepath = item->whatsThis(0);
if (QFile::exists(filepath))
{
createTextTab(filepath, QFileInfo(filepath).fileName());
}
}
// 選択中のタブを消す
void MainWindow::closeFile()
{
int cur = ui->tabWidget->currentIndex();
ui->tabWidget->removeTab(cur);
}
// 指定された番号のタブのファイルの保存
void MainWindow::saveFile(int i)
{
QString path = ui->tabWidget->tabWhatsThis(i);
QFile file(path);
if (file.open(QIODevice::WriteOnly | QIODevice::Text) == false)
{
return;
}
QTextStream out(&file);
out << ((QTextEdit*)(ui->tabWidget->widget(i)))->toPlainText();
file.close();
statusBar()->showMessage("ファイルを保存しました", 3000);
// タブのタイトルを変える
QString title = ui->tabWidget->tabText(i);
ui->tabWidget->setTabText(i, title.remove('*'));
}
// 選択中のタブのファイルを保存
void MainWindow::saveFile()
{
int cur = ui->tabWidget->currentIndex();
saveFile(cur);
}
// 開かれているすべてのファイルを保存
void MainWindow::saveAllFile()
{
for (int i = 0; i < ui->tabWidget->count(); i++)
{
saveFile(i);
}
}
// プロジェクトビューを右クリックしたとき、メニューを表示する
void MainWindow::showProjectViewContextMenu(const QPoint& point)
{
QMenu* menu = NULL;
QTreeWidgetItem* item = ui->treeWidget->currentItem();
if (item == NULL) return;
if (item->parent() == NULL)
{
if (item->whatsThis(0) != LIBRARY)
{
// ライブラリ以外のフォルダ
menu = p_projectTreeMenu;
}
}
else if (item->parent()->parent() == NULL)
{
// ライブラリ以外のフォルダ内のディレクトリ
menu = f_projectTreeMenu;
}
else if (item->parent()->parent()->parent() == NULL)
{
if (getProject(item)->whatsThis(0) == LIBRARY)
{
// ライブラリのソースコード
menu = lib_s_projectTreeMenu;
}
else
{
// ライブラリ以外のフォルダのソースコード
menu = s_projectTreeMenu;
}
}
if (menu)
{
// メニューを表示
menu->exec(ui->treeWidget->viewport()->mapToGlobal(point));
}
}