13495
|
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 "FileEditorDockWidget.h" |
|
20 #include <QVBoxLayout> |
|
21 #include <QApplication> |
|
22 #include <QFile> |
|
23 #include <QFileDialog> |
|
24 #include <QMessageBox> |
|
25 #include <QAction> |
|
26 |
|
27 FileEditorDockWidget::FileEditorDockWidget(QWidget *parent) |
|
28 : QDockWidget(parent) { |
|
29 construct(); |
|
30 } |
|
31 |
|
32 void FileEditorDockWidget::loadFile(QString fileName) { |
|
33 m_fileName = fileName; |
|
34 setWindowTitle(fileName); |
|
35 m_simpleEditor->load(fileName); |
|
36 } |
|
37 |
|
38 void FileEditorDockWidget::newFile() { |
|
39 if(m_modified) { |
|
40 int decision |
|
41 = QMessageBox::question(this, |
|
42 "Open New File", |
|
43 "Do you want to save the current file?", |
|
44 QMessageBox::Yes, QMessageBox::No); |
|
45 |
|
46 if(decision == QMessageBox::Yes) { |
|
47 saveFile(); |
|
48 if(m_modified) { |
|
49 // If the user attempted to save the file, but it's still |
|
50 // modified, then probably something went wrong, so we quit here. |
|
51 return; |
|
52 } |
|
53 } |
|
54 } |
|
55 |
|
56 m_fileName = "<unnamed>"; |
|
57 setWindowTitle(m_fileName); |
|
58 m_simpleEditor->setPlainText(""); |
|
59 } |
|
60 |
|
61 void FileEditorDockWidget::saveFile() { |
|
62 QString saveFileName = QFileDialog::getSaveFileName(this, "Save File", m_fileName); |
|
63 if(saveFileName.isEmpty()) |
|
64 return; |
|
65 |
|
66 QFile file(saveFileName); |
|
67 file.open(QFile::WriteOnly); |
|
68 |
|
69 if(file.write(m_simpleEditor->toPlainText().toLocal8Bit()) == -1) { |
|
70 QMessageBox::warning(this, |
|
71 "Error Saving File", |
|
72 QString("The file could not be saved: %1.").arg(file.errorString())); |
|
73 } else { |
|
74 m_simpleEditor->document()->setModified(false); |
|
75 } |
|
76 |
|
77 file.close(); |
|
78 } |
|
79 |
|
80 void FileEditorDockWidget::showToolTipNew() { |
|
81 m_statusBar->showMessage("Create a new file.", 2000); |
|
82 } |
|
83 |
|
84 void FileEditorDockWidget::showToolTipSave() { |
|
85 m_statusBar->showMessage("Save the file.", 2000); |
|
86 } |
|
87 |
|
88 void FileEditorDockWidget::showToolTipUndo() { |
|
89 m_statusBar->showMessage("Revert previous changes.", 2000); |
|
90 } |
|
91 |
|
92 void FileEditorDockWidget::showToolTipRedo() { |
|
93 m_statusBar->showMessage("Append previous changes.", 2000); |
|
94 } |
|
95 |
|
96 void FileEditorDockWidget::registerModified(bool modified) { |
|
97 m_modified = modified; |
|
98 } |
|
99 |
|
100 void FileEditorDockWidget::construct() { |
|
101 QStyle *style = QApplication::style(); |
|
102 setWidget(new QWidget()); |
|
103 m_toolBar = new QToolBar(this); |
|
104 m_simpleEditor = new SimpleEditor(this); |
|
105 m_statusBar = new QStatusBar(this); |
|
106 m_numberedTextView = new NumberedCodeEdit(this, m_simpleEditor); |
|
107 |
|
108 m_simpleEditor->setFont(QFont("Courier")); |
|
109 m_simpleEditor->setLineWrapMode(QPlainTextEdit::NoWrap); |
|
110 |
|
111 QAction *newAction = new QAction(style->standardIcon(QStyle::SP_FileIcon), |
|
112 "", m_toolBar); |
|
113 QAction *saveAction = new QAction(style->standardIcon(QStyle::SP_DriveHDIcon), |
|
114 "", m_toolBar); |
|
115 QAction *undoAction = new QAction(style->standardIcon(QStyle::SP_ArrowLeft), |
|
116 "", m_toolBar); |
|
117 QAction *redoAction = new QAction(style->standardIcon(QStyle::SP_ArrowRight), |
|
118 "", m_toolBar); |
|
119 |
|
120 m_toolBar->addAction(newAction); |
|
121 m_toolBar->addAction(saveAction); |
|
122 m_toolBar->addAction(undoAction); |
|
123 m_toolBar->addAction(redoAction); |
|
124 |
|
125 QVBoxLayout *layout = new QVBoxLayout(); |
|
126 layout->addWidget(m_toolBar); |
|
127 layout->addWidget(m_numberedTextView); |
|
128 layout->addWidget(m_statusBar); |
|
129 layout->setMargin(2); |
|
130 widget()->setLayout(layout); |
|
131 |
|
132 connect(newAction, SIGNAL(triggered()), this, SLOT(newFile())); |
|
133 connect(undoAction, SIGNAL(triggered()), m_simpleEditor, SLOT(undo())); |
|
134 connect(redoAction, SIGNAL(triggered()), m_simpleEditor, SLOT(redo())); |
|
135 connect(saveAction, SIGNAL(triggered()), this, SLOT(saveFile())); |
|
136 |
|
137 connect(newAction, SIGNAL(hovered()), this, SLOT(showToolTipNew())); |
|
138 connect(undoAction, SIGNAL(hovered()), this, SLOT(showToolTipUndo())); |
|
139 connect(redoAction, SIGNAL(hovered()), this, SLOT(showToolTipRedo())); |
|
140 connect(saveAction, SIGNAL(hovered()), this, SLOT(showToolTipSave())); |
|
141 |
|
142 connect(m_simpleEditor, SIGNAL(modificationChanged(bool)), this, SLOT(registerModified(bool))); |
|
143 |
|
144 m_fileName = ""; |
|
145 setWindowTitle(m_fileName); |
|
146 } |