comparison gui/src/FileEditorMdiSubWindow.cpp @ 13501:86d6c3b90ad7

Added new gui files.
author Jacob Dawid <jacob.dawid@googlemail.com>
date Sun, 17 Jul 2011 17:45:05 +0200
parents
children 5ab40ef3d241
comparison
equal deleted inserted replaced
13500:40bd465b6c79 13501:86d6c3b90ad7
1 /* Quint - A graphical user interface for Octave
2 * Copyright (C) 2011 Jacob Dawid
3 * jacob.dawid@googlemail.com
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "FileEditorMdiSubWindow.h"
20 #include <QVBoxLayout>
21 #include <QApplication>
22 #include <QFile>
23 #include <QFileDialog>
24 #include <QMessageBox>
25 #include <QAction>
26 #include <QStyle>
27 #include <QTextStream>
28
29 FileEditorMdiSubWindow::FileEditorMdiSubWindow(QWidget *parent)
30 : QMdiSubWindow(parent) {
31 construct();
32 }
33
34 FileEditorMdiSubWindow::~FileEditorMdiSubWindow() {
35 while(checkFileModified("Close File")) {
36 } // don't close if something went wrong while saving the file
37 }
38
39 void FileEditorMdiSubWindow::loadFile(QString fileName) {
40 QFile file(fileName);
41 if (!file.open(QFile::ReadOnly)) {
42 QMessageBox::warning(this, tr("File Editor"),
43 tr("Cannot read file %1:\n%2.")
44 .arg(fileName)
45 .arg(file.errorString()));
46 return;
47 }
48
49 QTextStream in(&file);
50 QApplication::setOverrideCursor(Qt::WaitCursor);
51 m_editor->setText(in.readAll());
52 QApplication::restoreOverrideCursor();
53
54 m_fileName = fileName;
55 setWindowTitle(fileName);
56 m_statusBar->showMessage(tr("File loaded."), 2000);
57 }
58
59 void FileEditorMdiSubWindow::newFile() {
60 if(checkFileModified("Open New File")) {
61 return; // something went wrong while saving the old file
62 }
63 m_fileName = "<unnamed>";
64 setWindowTitle(m_fileName);
65 //m_simpleEditor->setPlainText("");
66 }
67
68 int FileEditorMdiSubWindow::checkFileModified(QString msg) {
69 if(m_modified) {
70 int decision
71 = QMessageBox::question(this,
72 msg,
73 "Do you want to save the current file?",
74 QMessageBox::Yes, QMessageBox::No);
75
76 if(decision == QMessageBox::Yes) {
77 saveFile();
78 if(m_modified) {
79 // If the user attempted to save the file, but it's still
80 // modified, then probably something went wrong, so return error
81 return(1);
82 }
83 }
84 }
85 return(0);
86 }
87
88 void FileEditorMdiSubWindow::saveFile() {
89 if(m_fileName.isEmpty()) {
90 m_fileName = QFileDialog::getSaveFileName(this, "Save File", m_fileName);
91 if(m_fileName.isEmpty())
92 return;
93 }
94
95 QFile file(m_fileName);
96 if (!file.open(QFile::WriteOnly)) {
97 QMessageBox::warning(this, tr("File Editor"),
98 tr("Cannot write file %1:\n%2.")
99 .arg(m_fileName)
100 .arg(file.errorString()));
101 return;
102 }
103
104 QTextStream out(&file);
105 QApplication::setOverrideCursor(Qt::WaitCursor);
106 out << m_editor->text();
107 QApplication::restoreOverrideCursor();
108 m_statusBar->showMessage(tr("File saved"), 2000);
109 }
110
111 void FileEditorMdiSubWindow::saveFileAs() {/*
112 QString saveFileName = QFileDialog::getSaveFileName(this, "Save File", m_fileName);
113 if(saveFileName.isEmpty())
114 return;
115
116 QFile file(saveFileName);
117 file.open(QFile::WriteOnly);
118
119 if(file.write(m_simpleEditor->toPlainText().toLocal8Bit()) == -1) {
120 QMessageBox::warning(this,
121 "Error Saving File",
122 QString("The file could not be saved: %1.").arg(file.errorString()));
123 } else {
124 m_simpleEditor->document()->setModified(false);
125 m_fileName = saveFileName;
126 setWindowTitle(m_fileName);
127 }
128
129 file.close();*/
130 }
131
132 void FileEditorMdiSubWindow::showToolTipNew() {
133 m_statusBar->showMessage("Create a new file.", 2000);
134 }
135
136 void FileEditorMdiSubWindow::showToolTipSave() {
137 m_statusBar->showMessage("Save the file.", 2000);
138 }
139
140 void FileEditorMdiSubWindow::showToolTipSaveAs() {
141 m_statusBar->showMessage("Save the file as.", 2000);
142 }
143 void FileEditorMdiSubWindow::showToolTipUndo() {
144 m_statusBar->showMessage("Revert previous changes.", 2000);
145 }
146
147 void FileEditorMdiSubWindow::showToolTipRedo() {
148 m_statusBar->showMessage("Append previous changes.", 2000);
149 }
150
151 void FileEditorMdiSubWindow::registerModified(bool modified) {
152 m_modified = modified;
153 }
154
155 void FileEditorMdiSubWindow::construct() {
156 QStyle *style = QApplication::style();
157 setWidget(new QWidget());
158 m_toolBar = new QToolBar(this);
159 m_statusBar = new QStatusBar(this);
160 m_editor = new QsciScintilla(this);
161 m_editor->setMarginLineNumbers(QsciScintilla::TextMargin, true);
162 m_editor->setMarginWidth(QsciScintilla::TextMargin, "xxxx");
163
164 QAction *newAction = new QAction(style->standardIcon(QStyle::SP_FileIcon),
165 "", m_toolBar);
166 QAction *saveAction = new QAction(style->standardIcon(QStyle::SP_DriveHDIcon),
167 "", m_toolBar);
168 QAction *saveActionAs = new QAction(style->standardIcon(QStyle::SP_DriveFDIcon),
169 "", m_toolBar);
170 QAction *undoAction = new QAction(style->standardIcon(QStyle::SP_ArrowLeft),
171 "", m_toolBar);
172 QAction *redoAction = new QAction(style->standardIcon(QStyle::SP_ArrowRight),
173 "", m_toolBar);
174
175 m_toolBar->addAction(newAction);
176 m_toolBar->addAction(saveAction);
177 m_toolBar->addAction(saveActionAs);
178 m_toolBar->addAction(undoAction);
179 m_toolBar->addAction(redoAction);
180
181 QVBoxLayout *layout = new QVBoxLayout();
182 layout->addWidget(m_toolBar);
183 layout->addWidget(m_editor);
184 layout->addWidget(m_statusBar);
185 layout->setMargin(2);
186 widget()->setLayout(layout);
187
188 connect(newAction, SIGNAL(triggered()), this, SLOT(newFile()));
189 connect(undoAction, SIGNAL(triggered()), m_editor, SLOT(undo()));
190 connect(redoAction, SIGNAL(triggered()), m_editor, SLOT(redo()));
191 connect(saveAction, SIGNAL(triggered()), this, SLOT(saveFile()));
192 connect(saveActionAs, SIGNAL(triggered()), this, SLOT(saveFileAs()));
193
194 connect(newAction, SIGNAL(hovered()), this, SLOT(showToolTipNew()));
195 connect(undoAction, SIGNAL(hovered()), this, SLOT(showToolTipUndo()));
196 connect(redoAction, SIGNAL(hovered()), this, SLOT(showToolTipRedo()));
197 connect(saveAction, SIGNAL(hovered()), this, SLOT(showToolTipSave()));
198 connect(saveActionAs, SIGNAL(hovered()), this, SLOT(showToolTipSaveAs()));
199
200 m_fileName = "";
201 setWindowTitle(m_fileName);
202 show();
203 }