-
Notifications
You must be signed in to change notification settings - Fork 2
/
projectwindow.cpp
169 lines (154 loc) · 4.63 KB
/
projectwindow.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
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "projectwindow.h"
#include "sourceframe.h"
#include "settings.h"
#include <cstdio>
#include <QMenu>
#include <QFileDialog>
#include <QMessageBox>
extern SourceFrame *sourceFrame;
ProjectWindow::ProjectWindow(QWidget *parent)
: QFrame(parent)
{
setObjectName("Project");
setToolTip(tr("Right click to see project options"));
setFrameStyle(QFrame::Panel | QFrame::Raised);
setLineWidth(4);
QHBoxLayout *layout = new QHBoxLayout();
layout->setContentsMargins(10, 10, 10, 10);
list = new QListWidget();
layout->addWidget(list);
setLayout(layout);
}
QSize ProjectWindow::sizeHint() const
{
return QSize(200, 10);
}
void ProjectWindow::ignore()
{
}
void ProjectWindow::contextMenuEvent(QContextMenuEvent * /* event */)
{
if (projectFileName == "") {
QMenu menu(tr("Project menu"));
menu.addAction(tr("New project"), this, SLOT(newProject()));
menu.addAction(tr("Open project"), this, SLOT(openProject()));
menu.addAction(tr("ignore"), this, SLOT(ignore()));
menu.exec(QCursor::pos());
} else {
QMenu menu(tr("Project menu"));
menu.addAction(tr("Add file to project"), this, SLOT(addFile()));
menu.addAction(tr("Open file in editor"), this, SLOT(editFile()));
menu.addAction(tr("Drop file from project"), this, SLOT(dropFile()));
menu.addAction(tr("Close project"), this, SLOT(closeProject()));
menu.addAction(tr("ignore"), this, SLOT(ignore()));
menu.exec(QCursor::pos());
}
}
void ProjectWindow::newProject()
{
if (projectFileName != "") closeProject();
QString filename = QFileDialog::getSaveFileName(this,
tr("Select project name"), 0, tr("Projects (*.ebe)"));
if (filename != "") {
if (filename.right(4) != ".ebe") {
//qDebug() << "append .ebe";
filename += ".ebe";
}
QFile file(filename);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return;
file.resize(0);
projectFileName = filename;
}
}
void ProjectWindow::addFile(QString name)
{
if (projectFileName == "") return;
fileNames.append(QDir::current().absoluteFilePath(name));
list->addItem(QDir::current().relativeFilePath(name));
save();
}
void ProjectWindow::open(QString filename)
{
bool autoOpen = ebe["project/auto_open"].toBool();
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
QTextStream in(&file);
QString name;
name = in.readLine();
int i = 0;
while (!name.isNull()) {
fileNames.append(QDir::current().absoluteFilePath(name));
list->addItem(QDir::current().relativeFilePath(name));
if (autoOpen) {
//qDebug() << "opening " << name;
sourceFrame->open(name, i);
i++;
}
name = in.readLine();
}
projectFileName = filename;
}
void ProjectWindow::save()
{
QFile file(projectFileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::critical(this, tr("Error"),
tr("Failed to open ") + projectFileName + tr(" for writing"));
return;
}
QTextStream out(&file);
foreach ( QString name, fileNames ) {
out << name << endl;
}
file.close();
}
void ProjectWindow::dropFile()
{
QString file = QDir::current().absoluteFilePath(
list->currentItem()->text());
sourceFrame->close(file);
list->takeItem(list->currentRow());
fileNames.removeOne(file);
save();
}
void ProjectWindow::editFile()
{
QString file = QDir::current().absoluteFilePath(
list->currentItem()->text());
sourceFrame->open(file);
}
void ProjectWindow::addFile()
{
sourceFrame->open(true);
}
void ProjectWindow::openProject()
{
bool autoOpen = ebe["project/auto_open"].toBool();
if (projectFileName != "") closeProject();
QString filename = QFileDialog::getOpenFileName(this,
tr("Select project name"), 0, tr("Projects (*.ebe)"));
if (filename != "") {
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
QTextStream in(&file);
QString name;
name = in.readLine();
int i = 0;
while (!name.isNull()) {
fileNames.append(QDir::current().absoluteFilePath(name));
list->addItem(QDir::current().relativeFilePath(name));
if (autoOpen) {
sourceFrame->open(name, i);
i++;
}
name = in.readLine();
}
projectFileName = filename;
}
}
void ProjectWindow::closeProject()
{
projectFileName = "";
list->clear();
sourceFrame->closeTabs();
}