comparison libgui/src/m-editor/file-editor.cc @ 15848:424edeca3c66

Redo portions of file editor to use more signals/slots rather than casting. * file-editor-tab.cc, file-editor-tab.h (file_editor_tab::~file_editor_tab): Add. Delete lexer to prevent memory leak. Delete _edit_area to prevent memory leak. (file_editor_tab::conditional_close): Add. Simple slot that uses QWidget pointer as unique ID, not for function call. (file_editor_tab::file_name_query): Add. Simple slot that signals file name to whomever is connected. (file_editor_tab::find, file_editor_tab : public QWidget): Removed use of exec() and keep a pointer to the find_dialog as a member. Toggle hide()/show() via a connected slot to toggle visibility as desired. * file-editor.cc, file-editor.h, file-editor-tab.cc file-editor-tab.h (file_editor : public file_editor_interface, file_editor_tab : public QWidget, file_editor_tab::file_editor_tab, file_editor_tab::closeEvent, file_editor_tab::load_file, file_editor_tab::new_file, file_editor_tab::run_file): Remove _file_editor pointer member from file_editor_tab and rid file_editor::get_main_window from file_editor. There should be no need for such information about higher-level hierarchy inside lower-level objects. (file_editor::request_open_file, file_editor_tab::open_file): Move QFileDialog to file_editor::request_open_file and delete file_editor_tab::open_file since most of the remaining functionality is in file_editor_tab::load_file. (file_editor::active_editor_tab): Deleted. (file_editor::fetab_change_request, file_editor_tab::change_editor_state): Added to initiate a request for the editor tab to change focus. (file_editor_tab::editor_state_changed): Added arguments to pass the copy status and the directory path so that editor doesn't have to call functions for such information. (file_editor::handle_editor_state_changed): Add copying directory of the file_editor_tab to the current editing directory. (file_editor::check_conflict_save, file_editor_tab::editor_check_conflict_save, file_editor_tab::save_file_as, file_editor_tab::handle_save_file_as_answer): Moved a portion of the save-file-as dialog mechanism to the file_editor where all file names can be obtained to check for conflict with already open files. The new signal indicates to the editor that a name check be done, and in turn the editor signals the tab to save the file. * main-window.cc, file-editor.cc, file-editor.h, file-editor-interface.h (file_editor::terminal, file_editor : public file_editor_interface, file_editor_interface : public QDockWidget): Since file_editor_tab no longer looks up to main_window, remove _main_window and _terminal from file_editor and file_editor_interface, as well as file_editor::terminal. * file-editor-tab.cc (file_editor_tab::file_has_changed): Make the dialog boxes non-modal and use slots to handle user answers. (file_editor_tab::closeEvent): Remove portion that accesses upper hierarchy member functions, can find better approaches. (file_editor_tab::file_editor_tab): Make there no parent for QsciScintilla so that window modality can be set to individual editor. * file-editor-tab.cc, file-editor.cc (file_editor_tab::load_file): Use show() member rather than exec() and set modality to window so that rest of application may function. Return a QString with message rather than a boolean. * file-editor-tab.cc, (file_editor_tab::file_has_changed): Remove static variable alreadyAsking. Multiple file_editor_tabs are using this code so do not want to block recognition of multiple file having changed on disk (bug #37406). Instead, simply stop tracking via the file watcher. (file_editor_tab::save_file, file_editor_tab::save_file_as, file_editor_tab::handle_save_file_as_answer, file_editor_tab::handle_save_file_as_answer_close): Added a remove_on_success variable. Changed the QFileDialog to WindowModal and created slots to handle file selected signal and finished signal. Signal/slot connects vary based upon remove_on_success setting. (file_editor_tab::check_file_modified): Changed the QFileDialog to NonModal and attach some slots. Editor tab can't be parent in case deleted, so use read-only state of the editor area. * file-editor-tab.h (file_editor_tab : public QWidget): New signals for file_editor for tab and file name management. (file_editor_tab::get_file_name): Delete. * file-editor.h (file_editor : public file_editor_interface): Make QStringList sessionFileNames a member of file_editor so that it can retain data between file_editor_tab signals. Also can be used for checking precense of filenames and prevent opening multiple copies (bug #36869) Added signals for file editor tabs--settings_changed, fetab_close_request, and query_file_name. Three new slots for tab and file name management. * file-editor-interface.h, file-editor.h (file_editor_interface::add_file_editor_tab, file_editor::add_file_editor_tab): Made the text name for the tab an input variable. * file-editor.cc (file_editor::~file_editor): Replace dynamic_cast with simple signal querying all file editor tabs for file names which end up in savedSessionTabs. (file_editor::handle_file_name_changed): Dynamic cast not necessary since QObject and QWidget are compatible. (file_editor::handle_tab_close_request): Replace dynamic_cast with signal to request file_editor_tab with associated tabID tag should close. (file_editor::handle_tab_remove_request): Rename of handle_tab_close_request. Instead of dynamic cast, loop through pointers comparing QWidget* to QObject*, if same tag remove tab via index and also delete which fixes a memory leak. (file_editor::handle_add_filename_to_list): Simple slot that uses append() of the list member functions. (file_editor::notice_settings): Rather than dynamic cast, emit signal for the file_editor_tabs. (file_editor::add_file_editor_tab): New variety of connections for improved flow. (file_editor::request_open_file): Given error messages are made WindowModal, the tab shouldn't be delete if file open is not successful. The file_editor_tab takes care of that. (file_editor::request_open_file): Added check and message box for the requested file already open in editor. For the non-interactive overloaded version, open a message dialog box to tell the user file not found, e.g., could not find file in the settings when launched. (file_editor::request_open_file): Inquire file names and update list before checking for existence of files. Supply empty title to editor tab then have file_editor_tab update name. * file-editor-tab.h, file-editor-tab.cc, file-editor.cc (file_editor_tab::run_file): New signal process_octave_code. (file_editor::add_file_editor_tab): Connect signal process_octave_code to file_editor's parent's slot handle_command_double_clicked.
author Daniel J Sebald <daniel.sebald@ieee.org>
date Sun, 23 Dec 2012 14:33:48 -0600
parents bbbb89cc338f
children feba9ff6e6a8
comparison
equal deleted inserted replaced
15847:13d1e9bfa362 15848:424edeca3c66
33 #include <QFileDialog> 33 #include <QFileDialog>
34 #include <QMessageBox> 34 #include <QMessageBox>
35 #include <QStyle> 35 #include <QStyle>
36 #include <QTextStream> 36 #include <QTextStream>
37 37
38 file_editor::file_editor (QTerminal *t, main_window *m) 38 #include "octave-link.h"
39 : file_editor_interface (t, m) 39
40 { 40 file_editor::file_editor (QWidget *p)
41 : file_editor_interface (p)
42 {
43 // Set current editing directory before construct because loaded
44 // files will change ced accordingly.
45 ced = QDir::currentPath ();
46
41 construct (); 47 construct ();
42 48
43 _terminal = t;
44 _main_window = m;
45 setVisible (false); 49 setVisible (false);
46 } 50 }
47 51
48 file_editor::~file_editor () 52 file_editor::~file_editor ()
49 { 53 {
50 QSettings *settings = resource_manager::get_settings (); 54 QSettings *settings = resource_manager::get_settings ();
51 QStringList sessionFileNames; 55 fetFileNames.clear ();
52 if (settings->value ("editor/restoreSession",true).toBool ()) 56 if (settings->value ("editor/restoreSession",true).toBool ())
53 { 57 {
54 for (int n=0;n<_tab_widget->count();++n) 58 // Have all file editor tabs signal what their file names are.
55 { 59 emit fetab_file_name_query (0);
56 file_editor_tab* tab = dynamic_cast<file_editor_tab*> (_tab_widget->widget (n)); 60 }
57 if (!tab) 61 settings->setValue ("editor/savedSessionTabs", fetFileNames);
58 continue;
59 sessionFileNames.append (tab->get_file_name ());
60 }
61 }
62 settings->setValue ("editor/savedSessionTabs", sessionFileNames);
63 settings->sync (); 62 settings->sync ();
64 }
65
66 QTerminal *
67 file_editor::terminal ()
68 {
69 return _terminal;
70 }
71
72 main_window *
73 file_editor::get_main_window ()
74 {
75 return _main_window;
76 } 63 }
77 64
78 QMenu * 65 QMenu *
79 file_editor::debug_menu () 66 file_editor::debug_menu ()
80 { 67 {
100 } 87 }
101 88
102 void 89 void
103 file_editor::request_new_file () 90 file_editor::request_new_file ()
104 { 91 {
105 file_editor_tab *fileEditorTab = new file_editor_tab (this); 92 // New file isn't a file_editor_tab function since the file
93 // editor tab has yet to be created and there is no object to
94 // pass a signal to. Hence, functionality is here.
95
96 file_editor_tab *fileEditorTab = new file_editor_tab (ced);
106 if (fileEditorTab) 97 if (fileEditorTab)
107 { 98 {
108 add_file_editor_tab (fileEditorTab); 99 add_file_editor_tab (fileEditorTab, UNNAMED_FILE);
109 fileEditorTab->new_file (); 100 fileEditorTab->new_file ();
110 } 101 }
111 } 102 }
112 103
113 void 104 void
114 file_editor::request_open_file () 105 file_editor::request_open_file ()
115 { 106 {
116 file_editor_tab *current_tab = active_editor_tab (); 107 // Open file isn't a file_editor_tab function since the file
117 int curr_tab_index = _tab_widget->currentIndex (); 108 // editor tab has yet to be created and there is no object to
118 file_editor_tab *fileEditorTab = new file_editor_tab (this); 109 // pass a signal to. Hence, functionality is here.
119 if (fileEditorTab) 110
120 { 111 // Create a NonModal message.
121 add_file_editor_tab (fileEditorTab); 112 QFileDialog* fileDialog = new QFileDialog (this);
122 QString dir = QDir::currentPath (); 113 fileDialog->setNameFilter (SAVE_FILE_FILTER);
123 // get the filename of the last active tab to open a new file from there 114 fileDialog->setAcceptMode (QFileDialog::AcceptOpen);
124 if (current_tab) 115 fileDialog->setViewMode (QFileDialog::Detail);
125 dir = QDir::cleanPath (current_tab->get_file_name ()); 116 fileDialog->setDirectory (ced);
126 if (!fileEditorTab->open_file (dir)) 117 connect (fileDialog, SIGNAL (fileSelected (const QString&)),
118 this, SLOT (request_open_file (const QString&)));
119 fileDialog->setWindowModality (Qt::NonModal);
120 fileDialog->setAttribute (Qt::WA_DeleteOnClose);
121 fileDialog->show ();
122 }
123
124 void
125 file_editor::request_open_file (const QString& openFileName)
126 {
127 if (openFileName.isEmpty ())
128 {
129 // ?? Not sure this will happen. This routine isn't even called
130 // if the user hasn't selected a file.
131 }
132 else
133 {
134 // Have all file editor tabs signal what their file names are.
135 fetFileNames.clear ();
136 emit fetab_file_name_query (0);
137
138 // Check whether this file is already open in the editor.
139 if (fetFileNames.contains (openFileName, Qt::CaseSensitive))
127 { 140 {
128 // If no file was loaded, remove the tab again. 141 // Create a NonModal message so nothing is blocked and
129 _tab_widget->removeTab (_tab_widget->indexOf (fileEditorTab)); 142 // bring the existing file forward.
130 // restore focus to previous tab 143 QMessageBox* msgBox = new QMessageBox (
131 if (curr_tab_index>=0) 144 QMessageBox::Critical, tr ("Octave Editor"),
132 _tab_widget->setCurrentIndex (curr_tab_index); 145 tr ("File %1 is already open in the editor.").
146 arg (openFileName), QMessageBox::Ok, 0);
147 msgBox->setWindowModality (Qt::NonModal);
148 msgBox->setAttribute (Qt::WA_DeleteOnClose);
149 msgBox->show ();
150 for(int i = 0; i < _tab_widget->count (); i++)
151 {
152 if (_tab_widget->tabText (i) == openFileName)
153 {
154 _tab_widget->setCurrentIndex (i);
155 break;
156 }
157 }
158 return;
133 } 159 }
134 } 160
135 } 161 file_editor_tab *fileEditorTab = new file_editor_tab ();
136 162 if (fileEditorTab)
137 void
138 file_editor::request_open_file (const QString& fileName, bool silent)
139 {
140 if (!isVisible ())
141 {
142 show ();
143 }
144
145 file_editor_tab *fileEditorTab = new file_editor_tab (this);
146 int curr_tab_index = _tab_widget->currentIndex ();
147 if (fileEditorTab)
148 {
149 add_file_editor_tab (fileEditorTab);
150 if (!fileEditorTab->load_file (fileName, silent))
151 { 163 {
152 // If no file was loaded, remove the tab again. 164 QString result = fileEditorTab->load_file(openFileName);
153 _tab_widget->removeTab (_tab_widget->indexOf (fileEditorTab)); 165 if (result == "")
154 // restore focus to previous tab 166 {
155 _tab_widget->setCurrentIndex (curr_tab_index); 167 // Supply empty title then have the file_editor_tab update
168 // with full or short name.
169 add_file_editor_tab (fileEditorTab, "");
170 fileEditorTab->update_window_title (false);
171 }
172 else
173 {
174 delete fileEditorTab;
175 // Create a NonModal message about error.
176 QMessageBox* msgBox = new QMessageBox (
177 QMessageBox::Critical, tr ("Octave Editor"),
178 tr ("Could not open file %1 for read:\n%2.").
179 arg (openFileName).arg (result),
180 QMessageBox::Ok, 0);
181 msgBox->setWindowModality (Qt::NonModal);
182 msgBox->setAttribute (Qt::WA_DeleteOnClose);
183 msgBox->show ();
184 }
156 } 185 }
157 } 186 }
158 } 187 }
159 188
160 void 189 void
190 file_editor::check_conflict_save (const QString& saveFileName, bool remove_on_success)
191 {
192 // Have all file editor tabs signal what their file names are.
193 fetFileNames.clear ();
194 emit fetab_file_name_query (0);
195
196 // If one of those names matches the desired name, that's a conflict.
197 if (fetFileNames.contains (saveFileName, Qt::CaseSensitive))
198 {
199 // Note: to overwrite the contents of some other file editor tab
200 // with the same name requires identifying which file editor tab
201 // that is (not too difficult) then close that tab. Of course,
202 // that could trigger another dialog box if the file editor tab
203 // with the same name has modifications in it. This could become
204 // somewhat confusing to the user. For now, opt to do nothing.
205
206 // Create a NonModal message about error.
207 QMessageBox* msgBox = new QMessageBox (
208 QMessageBox::Critical, tr ("Octave Editor"),
209 tr ("File not saved! You've selected a file name\n\n %1\n\nwhich is the same as an already open file in the editor. (Could allow overwriting, with message, if that is what folks want.)").
210 arg (saveFileName),
211 QMessageBox::Ok, 0);
212 msgBox->setWindowModality (Qt::NonModal);
213 msgBox->setAttribute (Qt::WA_DeleteOnClose);
214 msgBox->show ();
215
216 return;
217 }
218
219 QObject* saveFileObject = sender ();
220 QWidget* saveFileWidget = 0;
221 for(int i = 0; i < _tab_widget->count (); i++)
222 {
223 if (_tab_widget->widget (i) == saveFileObject)
224 {
225 saveFileWidget = _tab_widget->widget (i);
226 break;
227 }
228 }
229 if (!saveFileWidget)
230 {
231 // Create a NonModal message about error.
232 QMessageBox* msgBox = new QMessageBox (
233 QMessageBox::Critical, tr ("Octave Editor"),
234 tr ("The associated file editor tab has disappeared. It was likely closed by some means."),
235 QMessageBox::Ok, 0);
236 msgBox->setWindowModality (Qt::NonModal);
237 msgBox->setAttribute (Qt::WA_DeleteOnClose);
238 msgBox->show ();
239 return;
240 }
241
242 // Can save without conflict, have the file editor tab do so.
243 emit fetab_save_file (saveFileWidget, saveFileName, remove_on_success);
244 }
245
246 void
161 file_editor::request_undo () 247 file_editor::request_undo ()
162 { 248 {
163 file_editor_tab *_active_file_editor_tab = active_editor_tab (); 249 emit fetab_undo (_tab_widget->currentWidget ());
164 if (_active_file_editor_tab)
165 _active_file_editor_tab->undo ();
166 } 250 }
167 251
168 void 252 void
169 file_editor::request_redo () 253 file_editor::request_redo ()
170 { 254 {
171 file_editor_tab *_active_file_editor_tab = active_editor_tab (); 255 emit fetab_redo (_tab_widget->currentWidget ());
172 if (_active_file_editor_tab)
173 _active_file_editor_tab->redo ();
174 } 256 }
175 257
176 void 258 void
177 file_editor::request_copy () 259 file_editor::request_copy ()
178 { 260 {
179 file_editor_tab *_active_file_editor_tab = active_editor_tab (); 261 emit fetab_copy (_tab_widget->currentWidget ());
180 if (_active_file_editor_tab)
181 _active_file_editor_tab->copy ();
182 } 262 }
183 263
184 void 264 void
185 file_editor::request_cut () 265 file_editor::request_cut ()
186 { 266 {
187 file_editor_tab *_active_file_editor_tab = active_editor_tab (); 267 emit fetab_cut (_tab_widget->currentWidget ());
188 if (_active_file_editor_tab)
189 _active_file_editor_tab->cut ();
190 } 268 }
191 269
192 void 270 void
193 file_editor::request_paste () 271 file_editor::request_paste ()
194 { 272 {
195 file_editor_tab *_active_file_editor_tab = active_editor_tab (); 273 emit fetab_paste (_tab_widget->currentWidget ());
196 if (_active_file_editor_tab)
197 _active_file_editor_tab->paste ();
198 } 274 }
199 275
200 void 276 void
201 file_editor::request_save_file () 277 file_editor::request_save_file ()
202 { 278 {
203 file_editor_tab *_active_file_editor_tab = active_editor_tab (); 279 emit fetab_save_file (_tab_widget->currentWidget ());
204 if (_active_file_editor_tab)
205 _active_file_editor_tab->save_file ();
206 } 280 }
207 281
208 void 282 void
209 file_editor::request_save_file_as () 283 file_editor::request_save_file_as ()
210 { 284 {
211 file_editor_tab *_active_file_editor_tab = active_editor_tab (); 285 emit fetab_save_file_as (_tab_widget->currentWidget ());
212 if (_active_file_editor_tab)
213 _active_file_editor_tab->save_file_as ();
214 } 286 }
215 287
216 void 288 void
217 file_editor::request_run_file () 289 file_editor::request_run_file ()
218 { 290 {
219 file_editor_tab *_active_file_editor_tab = active_editor_tab (); 291 emit fetab_run_file (_tab_widget->currentWidget ());
220 if (_active_file_editor_tab)
221 _active_file_editor_tab->run_file ();
222 } 292 }
223 293
224 void 294 void
225 file_editor::request_toggle_bookmark () 295 file_editor::request_toggle_bookmark ()
226 { 296 {
227 file_editor_tab *_active_file_editor_tab = active_editor_tab (); 297 emit fetab_toggle_bookmark (_tab_widget->currentWidget ());
228 if (_active_file_editor_tab)
229 _active_file_editor_tab->toggle_bookmark ();
230 } 298 }
231 299
232 void 300 void
233 file_editor::request_next_bookmark () 301 file_editor::request_next_bookmark ()
234 { 302 {
235 file_editor_tab *_active_file_editor_tab = active_editor_tab (); 303 emit fetab_next_bookmark (_tab_widget->currentWidget ());
236 if (_active_file_editor_tab)
237 _active_file_editor_tab->next_bookmark ();
238 } 304 }
239 305
240 void 306 void
241 file_editor::request_previous_bookmark () 307 file_editor::request_previous_bookmark ()
242 { 308 {
243 file_editor_tab *_active_file_editor_tab = active_editor_tab (); 309 emit fetab_previous_bookmark (_tab_widget->currentWidget ());
244 if (_active_file_editor_tab)
245 _active_file_editor_tab->previous_bookmark ();
246 } 310 }
247 311
248 void 312 void
249 file_editor::request_remove_bookmark () 313 file_editor::request_remove_bookmark ()
250 { 314 {
251 file_editor_tab *_active_file_editor_tab = active_editor_tab (); 315 emit fetab_remove_bookmark (_tab_widget->currentWidget ());
252 if (_active_file_editor_tab)
253 _active_file_editor_tab->remove_bookmark ();
254 } 316 }
255 317
256 void 318 void
257 file_editor::request_toggle_breakpoint () 319 file_editor::request_toggle_breakpoint ()
258 { 320 {
259 file_editor_tab *_active_file_editor_tab = active_editor_tab (); 321 emit fetab_toggle_breakpoint (_tab_widget->currentWidget ());
260 if (_active_file_editor_tab)
261 _active_file_editor_tab->toggle_breakpoint ();
262 } 322 }
263 323
264 void 324 void
265 file_editor::request_next_breakpoint () 325 file_editor::request_next_breakpoint ()
266 { 326 {
267 file_editor_tab *_active_file_editor_tab = active_editor_tab (); 327 emit fetab_next_breakpoint (_tab_widget->currentWidget ());
268 if (_active_file_editor_tab)
269 _active_file_editor_tab->next_breakpoint ();
270 } 328 }
271 329
272 void 330 void
273 file_editor::request_previous_breakpoint () 331 file_editor::request_previous_breakpoint ()
274 { 332 {
275 file_editor_tab *_active_file_editor_tab = active_editor_tab (); 333 emit fetab_previous_breakpoint (_tab_widget->currentWidget ());
276 if (_active_file_editor_tab)
277 _active_file_editor_tab->previous_breakpoint ();
278 } 334 }
279 335
280 void 336 void
281 file_editor::request_remove_breakpoint () 337 file_editor::request_remove_breakpoint ()
282 { 338 {
283 file_editor_tab *_active_file_editor_tab = active_editor_tab (); 339 emit fetab_remove_all_breakpoints (_tab_widget->currentWidget ());
284 if (_active_file_editor_tab)
285 _active_file_editor_tab->remove_all_breakpoints ();
286 } 340 }
287 341
288 void 342 void
289 file_editor::request_comment_selected_text () 343 file_editor::request_comment_selected_text ()
290 { 344 {
291 file_editor_tab *_active_file_editor_tab = active_editor_tab (); 345 emit fetab_comment_selected_text (_tab_widget->currentWidget ());
292 if (_active_file_editor_tab)
293 _active_file_editor_tab->comment_selected_text ();
294 } 346 }
295 347
296 void 348 void
297 file_editor::request_uncomment_selected_text () 349 file_editor::request_uncomment_selected_text ()
298 { 350 {
299 file_editor_tab *_active_file_editor_tab = active_editor_tab (); 351 emit fetab_uncomment_selected_text (_tab_widget->currentWidget ());
300 if (_active_file_editor_tab)
301 _active_file_editor_tab->uncomment_selected_text ();
302 } 352 }
303 353
304 void 354 void
305 file_editor::request_find () 355 file_editor::request_find ()
306 { 356 {
307 file_editor_tab *_active_file_editor_tab = active_editor_tab (); 357 emit fetab_find (_tab_widget->currentWidget ());
308 if (_active_file_editor_tab)
309 _active_file_editor_tab->find ();
310 } 358 }
311 359
312 void 360 void
313 file_editor::handle_file_name_changed (const QString& fileName) 361 file_editor::handle_file_name_changed (const QString& fileName)
314 { 362 {
315 QObject *senderObject = sender (); 363 QObject *fileEditorTab = sender();
316 file_editor_tab *fileEditorTab
317 = dynamic_cast<file_editor_tab*> (senderObject);
318 if (fileEditorTab) 364 if (fileEditorTab)
319 { 365 {
320 for(int i = 0; i < _tab_widget->count (); i++) 366 for(int i = 0; i < _tab_widget->count (); i++)
321 { 367 {
322 if (_tab_widget->widget (i) == fileEditorTab) 368 if (_tab_widget->widget (i) == fileEditorTab)
328 } 374 }
329 375
330 void 376 void
331 file_editor::handle_tab_close_request (int index) 377 file_editor::handle_tab_close_request (int index)
332 { 378 {
333 file_editor_tab *fileEditorTab 379 // Signal to the tabs a request to close whomever matches the identifying
334 = dynamic_cast <file_editor_tab*> (_tab_widget->widget (index)); 380 // tag (i.e., unique widget pointer). The reason for this indirection is
381 // that it will enable a file editor widget to toss up a non-static
382 // dialog box and later signal that it wants to be removed.
383 QWidget *tabID = _tab_widget->widget (index);
384 emit fetab_close_request (tabID);
385 }
386
387 void
388 file_editor::handle_tab_remove_request ()
389 {
390 QObject *fileEditorTab = sender();
335 if (fileEditorTab) 391 if (fileEditorTab)
336 if (fileEditorTab->close ()) 392 {
337 { 393 for(int i = 0; i < _tab_widget->count (); i++)
338 _tab_widget->removeTab (index); 394 {
339 delete fileEditorTab; 395 if (_tab_widget->widget (i) == fileEditorTab)
340 } 396 {
341 } 397 _tab_widget->removeTab (i);
342 398 delete fileEditorTab;
343 void 399 }
344 file_editor::handle_tab_close_request () 400 }
345 { 401 }
346 file_editor_tab *fileEditorTab = dynamic_cast <file_editor_tab*> (sender ()); 402 }
347 if (fileEditorTab) 403
348 if (fileEditorTab->close ()) 404 void
349 { 405 file_editor::handle_add_filename_to_list (const QString& fileName)
350 _tab_widget->removeTab (_tab_widget->indexOf (fileEditorTab)); 406 {
351 delete fileEditorTab; 407 fetFileNames.append (fileName);
352 } 408 }
409
410 void
411 file_editor::active_tab_changed (int index)
412 {
413 emit fetab_change_request (_tab_widget->widget (index));
414 }
415
416 void
417 file_editor::handle_editor_state_changed (bool copy_available, const QString& file_name)
418 {
419 // In case there is some scenario where traffic could be coming from
420 // all the file editor tabs, just process info from the current active tab.
421 if (sender() == _tab_widget->currentWidget ())
422 {
423 _copy_action->setEnabled (copy_available);
424 _cut_action->setEnabled (copy_available);
425 if (!file_name.isEmpty ())
426 {
427 ced = QDir::cleanPath (file_name);
428 int lastslash = ced.lastIndexOf ('/');
429 // Test against > 0 because if somehow the directory is "/" the
430 // slash should be retained. Otherwise, last slash is removed.
431 if (lastslash > 0 && lastslash != ced.count ())
432 {
433 ced = ced.left (lastslash);
434 }
435 }
436 setFocusProxy (_tab_widget->currentWidget ());
437 }
438 }
439
440 void
441 file_editor::notice_settings ()
442 {
443 // Relay signal to file editor tabs.
444 emit fetab_settings_changed ();
353 } 445 }
354 446
355 // slot for signal that is emitted when floating property changes 447 // slot for signal that is emitted when floating property changes
356 void 448 void
357 file_editor::top_level_changed (bool floating) 449 file_editor::top_level_changed (bool floating)
358 { 450 {
359 if(floating) 451 if(floating)
360 { 452 {
361 setWindowFlags(Qt::Window); // make a window from the widget when floating 453 setWindowFlags(Qt::Window); // make a window from the widget when floating
362 show(); // make it visible again since setWindowFlag hides it 454 show(); // make it visible again since setWindowFlag hides it
363 }
364 }
365
366 void
367 file_editor::active_tab_changed (int)
368 {
369 handle_editor_state_changed ();
370 }
371
372 void
373 file_editor::handle_editor_state_changed ()
374 {
375 file_editor_tab *f = active_editor_tab ();
376 if (f)
377 {
378 bool copy_available = f->copy_available ();
379 _copy_action->setEnabled (copy_available);
380 _cut_action->setEnabled (copy_available);
381 setFocusProxy (f);
382 }
383 }
384
385 void
386 file_editor::notice_settings ()
387 {
388 for(int i = 0; i < _tab_widget->count (); i++)
389 {
390 file_editor_tab *fileEditorTab
391 = dynamic_cast <file_editor_tab*> (_tab_widget->widget (i));
392 if (fileEditorTab)
393 fileEditorTab->notice_settings ();
394 } 455 }
395 } 456 }
396 457
397 void 458 void
398 file_editor::construct () 459 file_editor::construct ()
605 if (settings->value ("editor/restoreSession",true).toBool ()) 666 if (settings->value ("editor/restoreSession",true).toBool ())
606 { 667 {
607 QStringList sessionFileNames = settings->value("editor/savedSessionTabs", QStringList()).toStringList (); 668 QStringList sessionFileNames = settings->value("editor/savedSessionTabs", QStringList()).toStringList ();
608 669
609 for (int n=0; n < sessionFileNames.count (); ++n) 670 for (int n=0; n < sessionFileNames.count (); ++n)
610 request_open_file (sessionFileNames.at (n), true); 671 request_open_file (sessionFileNames.at (n));
611 } 672 }
612 } 673 }
613 674
614 void 675 void
615 file_editor::add_file_editor_tab (file_editor_tab *f) 676 file_editor::add_file_editor_tab (file_editor_tab *f, const QString &fn)
616 { 677 {
617 _tab_widget->addTab (f, ""); 678 _tab_widget->addTab (f, fn);
618 connect (f, SIGNAL (file_name_changed(QString)), 679
619 this, SLOT(handle_file_name_changed(QString))); 680 // Signals from the file editor_tab
620 connect (f, SIGNAL (editor_state_changed ()), 681 connect (f, SIGNAL (file_name_changed (const QString&)),
621 this, SLOT (handle_editor_state_changed ())); 682 this, SLOT (handle_file_name_changed (const QString&)));
622 connect (f, SIGNAL (close_request ()), 683 connect (f, SIGNAL (editor_state_changed (bool, const QString&)),
623 this, SLOT (handle_tab_close_request ())); 684 this, SLOT (handle_editor_state_changed (bool, const QString&)));
685 connect (f, SIGNAL (tab_remove_request ()),
686 this, SLOT (handle_tab_remove_request ()));
687 connect (f, SIGNAL (add_filename_to_list (const QString&)),
688 this, SLOT (handle_add_filename_to_list (const QString&)));
689 connect (f, SIGNAL (editor_check_conflict_save (const QString&, bool)),
690 this, SLOT (check_conflict_save (const QString&, bool)));
691 connect (f, SIGNAL (process_octave_code (const QString&)),
692 parent (), SLOT (handle_command_double_clicked (const QString&)));
693
694 // Signals from the file_editor non-trivial operations
695 connect (this, SIGNAL (fetab_settings_changed ()),
696 f, SLOT (notice_settings ()));
697 connect (this, SIGNAL (fetab_close_request (const QWidget*)),
698 f, SLOT (conditional_close (const QWidget*)));
699 connect (this, SIGNAL (fetab_change_request (const QWidget*)),
700 f, SLOT (change_editor_state (const QWidget*)));
701 connect (this, SIGNAL (fetab_file_name_query (const QWidget*)),
702 f, SLOT (file_name_query (const QWidget*)));
703 connect (this, SIGNAL (fetab_save_file (const QWidget*, const QString&, bool)),
704 f, SLOT (save_file (const QWidget*, const QString&, bool)));
705 // Signals from the file_editor trivial operations
706 connect (this, SIGNAL (fetab_undo (const QWidget*)),
707 f, SLOT (undo (const QWidget*)));
708 connect (this, SIGNAL (fetab_redo (const QWidget*)),
709 f, SLOT (redo (const QWidget*)));
710 connect (this, SIGNAL (fetab_copy (const QWidget*)),
711 f, SLOT (copy (const QWidget*)));
712 connect (this, SIGNAL (fetab_cut (const QWidget*)),
713 f, SLOT (cut (const QWidget*)));
714 connect (this, SIGNAL (fetab_paste (const QWidget*)),
715 f, SLOT (paste (const QWidget*)));
716 connect (this, SIGNAL (fetab_save_file (const QWidget*)),
717 f, SLOT (save_file (const QWidget*)));
718 connect (this, SIGNAL (fetab_save_file_as (const QWidget*)),
719 f, SLOT (save_file_as (const QWidget*)));
720 connect (this, SIGNAL (fetab_run_file (const QWidget*)),
721 f, SLOT (run_file (const QWidget*)));
722 connect (this, SIGNAL (fetab_toggle_bookmark (const QWidget*)),
723 f, SLOT (toggle_bookmark (const QWidget*)));
724 connect (this, SIGNAL (fetab_next_bookmark (const QWidget*)),
725 f, SLOT (next_bookmark (const QWidget*)));
726 connect (this, SIGNAL (fetab_previous_bookmark (const QWidget*)),
727 f, SLOT (previous_bookmark (const QWidget*)));
728 connect (this, SIGNAL (fetab_remove_bookmark (const QWidget*)),
729 f, SLOT (remove_bookmark (const QWidget*)));
730 connect (this, SIGNAL (fetab_toggle_breakpoint (const QWidget*)),
731 f, SLOT (toggle_breakpoint (const QWidget*)));
732 connect (this, SIGNAL (fetab_next_breakpoint (const QWidget*)),
733 f, SLOT (next_breakpoint (const QWidget*)));
734 connect (this, SIGNAL (fetab_previous_breakpoint (const QWidget*)),
735 f, SLOT (previous_breakpoint (const QWidget*)));
736 connect (this, SIGNAL (fetab_remove_all_breakpoints (const QWidget*)),
737 f, SLOT (remove_all_breakpoints (const QWidget*)));
738 connect (this, SIGNAL (fetab_comment_selected_text (const QWidget*)),
739 f, SLOT (comment_selected_text (const QWidget*)));
740 connect (this, SIGNAL (fetab_uncomment_selected_text (const QWidget*)),
741 f, SLOT (uncomment_selected_text (const QWidget*)));
742 connect (this, SIGNAL (fetab_find (const QWidget*)),
743 f, SLOT (find (const QWidget*)));
744
624 _tab_widget->setCurrentWidget (f); 745 _tab_widget->setCurrentWidget (f);
625 } 746 }
626
627 file_editor_tab *
628 file_editor::active_editor_tab ()
629 {
630 return dynamic_cast<file_editor_tab*> (_tab_widget->currentWidget ());
631 }