/
GuiView.cpp
3778 lines (3192 loc) · 95.7 KB
/
GuiView.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* \file GuiView.cpp
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Lars Gullik Bjønnes
* \author John Levon
* \author Abdelrazak Younes
* \author Peter Kümmel
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "GuiView.h"
#include "Dialog.h"
#include "DispatchResult.h"
#include "FileDialog.h"
#include "FontLoader.h"
#include "GuiApplication.h"
#include "GuiCommandBuffer.h"
#include "GuiCompleter.h"
#include "GuiKeySymbol.h"
#include "GuiToc.h"
#include "GuiToolbar.h"
#include "GuiWorkArea.h"
#include "LayoutBox.h"
#include "Menus.h"
#include "TocModel.h"
#include "qt_helpers.h"
#include "frontends/alert.h"
#include "buffer_funcs.h"
#include "Buffer.h"
#include "BufferList.h"
#include "BufferParams.h"
#include "BufferView.h"
#include "Compare.h"
#include "Converter.h"
#include "Cursor.h"
#include "CutAndPaste.h"
#include "Encoding.h"
#include "ErrorList.h"
#include "Format.h"
#include "FuncStatus.h"
#include "FuncRequest.h"
#include "Intl.h"
#include "Layout.h"
#include "Lexer.h"
#include "LyXAction.h"
#include "LyX.h"
#include "LyXRC.h"
#include "LyXVC.h"
#include "Paragraph.h"
#include "SpellChecker.h"
#include "TexRow.h"
#include "TextClass.h"
#include "Text.h"
#include "Toolbars.h"
#include "version.h"
#include "support/convert.h"
#include "support/debug.h"
#include "support/ExceptionMessage.h"
#include "support/FileName.h"
#include "support/filetools.h"
#include "support/gettext.h"
#include "support/filetools.h"
#include "support/ForkedCalls.h"
#include "support/lassert.h"
#include "support/lstrings.h"
#include "support/os.h"
#include "support/Package.h"
#include "support/Path.h"
#include "support/Systemcall.h"
#include "support/Timeout.h"
#include "support/ProgressInterface.h"
#include "GuiProgress.h"
#include <QAction>
#include <QApplication>
#include <QCloseEvent>
#include <QDebug>
#include <QDesktopWidget>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QList>
#include <QMenu>
#include <QMenuBar>
#include <QPainter>
#include <QPixmap>
#include <QPixmapCache>
#include <QPoint>
#include <QPushButton>
#include <QSettings>
#include <QShowEvent>
#include <QSplitter>
#include <QStackedWidget>
#include <QStatusBar>
#include <QTime>
#include <QTimer>
#include <QToolBar>
#include <QUrl>
#include <QScrollBar>
#define EXPORT_in_THREAD 1
// QtConcurrent was introduced in Qt 4.4
#if (QT_VERSION >= 0x040400)
#include <QFuture>
#include <QFutureWatcher>
#include <QtConcurrentRun>
#endif
#include "support/bind.h"
#include <sstream>
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
using namespace std;
using namespace lyx::support;
namespace lyx {
namespace frontend {
namespace {
class BackgroundWidget : public QWidget
{
public:
BackgroundWidget()
{
LYXERR(Debug::GUI, "show banner: " << lyxrc.show_banner);
if (!lyxrc.show_banner)
return;
/// The text to be written on top of the pixmap
QString const text = lyx_version ?
qt_("version ") + lyx_version : qt_("unknown version");
splash_ = getPixmap("images/", "banner", "png");
QPainter pain(&splash_);
pain.setPen(QColor(0, 0, 0));
QFont font;
// The font used to display the version info
font.setStyleHint(QFont::SansSerif);
font.setWeight(QFont::Bold);
font.setPointSize(int(toqstr(lyxrc.font_sizes[FONT_SIZE_LARGE]).toDouble()));
int width = QFontMetrics(font).width(text);
pain.setFont(font);
pain.drawText(397 - width, 15, text);
setFocusPolicy(Qt::StrongFocus);
}
void paintEvent(QPaintEvent *)
{
int x = (width() - splash_.width()) / 2;
int y = (height() - splash_.height()) / 2;
QPainter pain(this);
pain.drawPixmap(x, y, splash_);
}
void keyPressEvent(QKeyEvent * ev)
{
KeySymbol sym;
setKeySymbol(&sym, ev);
if (sym.isOK()) {
guiApp->processKeySym(sym, q_key_state(ev->modifiers()));
ev->accept();
} else {
ev->ignore();
}
}
private:
QPixmap splash_;
};
/// Toolbar store providing access to individual toolbars by name.
typedef map<string, GuiToolbar *> ToolbarMap;
typedef shared_ptr<Dialog> DialogPtr;
} // namespace anon
struct GuiView::GuiViewPrivate
{
GuiViewPrivate(GuiView * gv)
: gv_(gv), current_work_area_(0), current_main_work_area_(0),
layout_(0), autosave_timeout_(5000),
in_show_(false)
{
// hardcode here the platform specific icon size
smallIconSize = 14; // scaling problems
normalIconSize = 20; // ok, default
bigIconSize = 26; // better for some math icons
splitter_ = new QSplitter;
bg_widget_ = new BackgroundWidget;
stack_widget_ = new QStackedWidget;
stack_widget_->addWidget(bg_widget_);
stack_widget_->addWidget(splitter_);
setBackground();
// TODO cleanup, remove the singleton, handle multiple Windows?
progress_ = ProgressInterface::instance();
if (!dynamic_cast<GuiProgress*>(progress_)) {
progress_ = new GuiProgress(); // TODO who deletes it
ProgressInterface::setInstance(progress_);
}
QObject::connect(
dynamic_cast<GuiProgress*>(progress_),
SIGNAL(updateStatusBarMessage(QString const&)),
gv, SLOT(updateStatusBarMessage(QString const&)));
QObject::connect(
dynamic_cast<GuiProgress*>(progress_),
SIGNAL(clearMessageText()),
gv, SLOT(clearMessageText()));
}
~GuiViewPrivate()
{
delete splitter_;
delete bg_widget_;
delete stack_widget_;
}
QMenu * toolBarPopup(GuiView * parent)
{
// FIXME: translation
QMenu * menu = new QMenu(parent);
QActionGroup * iconSizeGroup = new QActionGroup(parent);
QAction * smallIcons = new QAction(iconSizeGroup);
smallIcons->setText(qt_("Small-sized icons"));
smallIcons->setCheckable(true);
QObject::connect(smallIcons, SIGNAL(triggered()),
parent, SLOT(smallSizedIcons()));
menu->addAction(smallIcons);
QAction * normalIcons = new QAction(iconSizeGroup);
normalIcons->setText(qt_("Normal-sized icons"));
normalIcons->setCheckable(true);
QObject::connect(normalIcons, SIGNAL(triggered()),
parent, SLOT(normalSizedIcons()));
menu->addAction(normalIcons);
QAction * bigIcons = new QAction(iconSizeGroup);
bigIcons->setText(qt_("Big-sized icons"));
bigIcons->setCheckable(true);
QObject::connect(bigIcons, SIGNAL(triggered()),
parent, SLOT(bigSizedIcons()));
menu->addAction(bigIcons);
unsigned int cur = parent->iconSize().width();
if ( cur == parent->d.smallIconSize)
smallIcons->setChecked(true);
else if (cur == parent->d.normalIconSize)
normalIcons->setChecked(true);
else if (cur == parent->d.bigIconSize)
bigIcons->setChecked(true);
return menu;
}
void setBackground()
{
stack_widget_->setCurrentWidget(bg_widget_);
bg_widget_->setUpdatesEnabled(true);
bg_widget_->setFocus();
}
TabWorkArea * tabWorkArea(int i)
{
return dynamic_cast<TabWorkArea *>(splitter_->widget(i));
}
TabWorkArea * currentTabWorkArea()
{
if (splitter_->count() == 1)
// The first TabWorkArea is always the first one, if any.
return tabWorkArea(0);
for (int i = 0; i != splitter_->count(); ++i) {
TabWorkArea * twa = tabWorkArea(i);
if (current_main_work_area_ == twa->currentWorkArea())
return twa;
}
// None has the focus so we just take the first one.
return tabWorkArea(0);
}
#if (QT_VERSION >= 0x040400)
void setPreviewFuture(QFuture<docstring> const & f)
{
if (preview_watcher_.isRunning()) {
// we prefer to cancel this preview in order to keep a snappy
// interface.
return;
}
preview_watcher_.setFuture(f);
}
#endif
public:
GuiView * gv_;
GuiWorkArea * current_work_area_;
GuiWorkArea * current_main_work_area_;
QSplitter * splitter_;
QStackedWidget * stack_widget_;
BackgroundWidget * bg_widget_;
/// view's toolbars
ToolbarMap toolbars_;
ProgressInterface* progress_;
/// The main layout box.
/**
* \warning Don't Delete! The layout box is actually owned by
* whichever toolbar contains it. All the GuiView class needs is a
* means of accessing it.
*
* FIXME: replace that with a proper model so that we are not limited
* to only one dialog.
*/
LayoutBox * layout_;
///
map<string, DialogPtr> dialogs_;
unsigned int smallIconSize;
unsigned int normalIconSize;
unsigned int bigIconSize;
///
QTimer statusbar_timer_;
/// auto-saving of buffers
Timeout autosave_timeout_;
/// flag against a race condition due to multiclicks, see bug #1119
bool in_show_;
///
TocModels toc_models_;
#if (QT_VERSION >= 0x040400)
///
QFutureWatcher<docstring> autosave_watcher_;
QFutureWatcher<docstring> preview_watcher_;
///
string last_export_format;
#else
struct DummyWatcher { bool isRunning(){return false;} };
DummyWatcher preview_watcher_;
#endif
static QSet<Buffer const *> busyBuffers;
static docstring previewAndDestroy(Buffer const * orig, Buffer * buffer, string const & format);
static docstring exportAndDestroy(Buffer const * orig, Buffer * buffer, string const & format);
static docstring saveAndDestroy(Buffer const * orig, Buffer * buffer, FileName const & fname);
};
QSet<Buffer const *> GuiView::GuiViewPrivate::busyBuffers;
GuiView::GuiView(int id)
: d(*new GuiViewPrivate(this)), id_(id), closing_(false)
{
// GuiToolbars *must* be initialised before the menu bar.
normalSizedIcons(); // at least on Mac the default is 32 otherwise, which is huge
constructToolbars();
// set ourself as the current view. This is needed for the menu bar
// filling, at least for the static special menu item on Mac. Otherwise
// they are greyed out.
guiApp->setCurrentView(this);
// Fill up the menu bar.
guiApp->menus().fillMenuBar(menuBar(), this, true);
setCentralWidget(d.stack_widget_);
// Start autosave timer
if (lyxrc.autosave) {
d.autosave_timeout_.timeout.connect(bind(&GuiView::autoSave, this));
d.autosave_timeout_.setTimeout(lyxrc.autosave * 1000);
d.autosave_timeout_.start();
}
connect(&d.statusbar_timer_, SIGNAL(timeout()),
this, SLOT(clearMessage()));
// We don't want to keep the window in memory if it is closed.
setAttribute(Qt::WA_DeleteOnClose, true);
#if (!defined(Q_WS_WIN) && !defined(Q_WS_MACX))
// assign an icon to main form. We do not do it under Qt/Win or Qt/Mac,
// since the icon is provided in the application bundle.
setWindowIcon(getPixmap("images/", "lyx", "png"));
#endif
#if (QT_VERSION >= 0x040300)
// use tabbed dock area for multiple docks
// (such as "source" and "messages")
setDockOptions(QMainWindow::ForceTabbedDocks);
#endif
// For Drag&Drop.
setAcceptDrops(true);
statusBar()->setSizeGripEnabled(true);
updateStatusBar();
#if (QT_VERSION >= 0x040400)
connect(&d.autosave_watcher_, SIGNAL(finished()), this,
SLOT(threadFinished()));
connect(&d.preview_watcher_, SIGNAL(finished()), this,
SLOT(threadFinished()));
#endif
connect(this, SIGNAL(triggerShowDialog(QString const &, QString const &, Inset *)),
SLOT(doShowDialog(QString const &, QString const &, Inset *)));
// Forbid too small unresizable window because it can happen
// with some window manager under X11.
setMinimumSize(300, 200);
if (lyxrc.allow_geometry_session) {
// Now take care of session management.
if (restoreLayout())
return;
}
// no session handling, default to a sane size.
setGeometry(50, 50, 690, 510);
initToolbars();
// clear session data if any.
QSettings settings;
settings.remove("views");
}
GuiView::~GuiView()
{
delete &d;
}
void GuiView::threadFinished()
{
#if (QT_VERSION >= 0x040400)
QFutureWatcher<docstring> const * watcher =
static_cast<QFutureWatcher<docstring> const *>(sender());
message(watcher->result());
updateToolbars();
errors(d.last_export_format);
#endif
}
void GuiView::saveLayout() const
{
QSettings settings;
settings.beginGroup("views");
settings.beginGroup(QString::number(id_));
#ifdef Q_WS_X11
settings.setValue("pos", pos());
settings.setValue("size", size());
#else
settings.setValue("geometry", saveGeometry());
#endif
settings.setValue("layout", saveState(0));
settings.setValue("icon_size", iconSize());
}
bool GuiView::restoreLayout()
{
QSettings settings;
settings.beginGroup("views");
settings.beginGroup(QString::number(id_));
QString const icon_key = "icon_size";
if (!settings.contains(icon_key))
return false;
//code below is skipped when when ~/.config/LyX is (re)created
setIconSize(settings.value(icon_key).toSize());
#ifdef Q_WS_X11
QPoint pos = settings.value("pos", QPoint(50, 50)).toPoint();
QSize size = settings.value("size", QSize(690, 510)).toSize();
resize(size);
move(pos);
#else
// Work-around for bug #6034: the window ends up in an undetermined
// state when trying to restore a maximized window when it is
// already maximized.
if (!(windowState() & Qt::WindowMaximized))
if (!restoreGeometry(settings.value("geometry").toByteArray()))
setGeometry(50, 50, 690, 510);
#endif
// Make sure layout is correctly oriented.
setLayoutDirection(qApp->layoutDirection());
// Allow the toc and view-source dock widget to be restored if needed.
Dialog * dialog;
if ((dialog = findOrBuild("toc", true)))
// see bug 5082. At least setup title and enabled state.
// Visibility will be adjusted by restoreState below.
dialog->prepareView();
if ((dialog = findOrBuild("view-source", true)))
dialog->prepareView();
if ((dialog = findOrBuild("progress", true)))
dialog->prepareView();
if (!restoreState(settings.value("layout").toByteArray(), 0))
initToolbars();
updateDialogs();
return true;
}
GuiToolbar * GuiView::toolbar(string const & name)
{
ToolbarMap::iterator it = d.toolbars_.find(name);
if (it != d.toolbars_.end())
return it->second;
LYXERR(Debug::GUI, "Toolbar::display: no toolbar named " << name);
return 0;
}
void GuiView::constructToolbars()
{
ToolbarMap::iterator it = d.toolbars_.begin();
for (; it != d.toolbars_.end(); ++it)
delete it->second;
d.toolbars_.clear();
// I don't like doing this here, but the standard toolbar
// destroys this object when it's destroyed itself (vfr)
d.layout_ = new LayoutBox(*this);
d.stack_widget_->addWidget(d.layout_);
d.layout_->move(0,0);
// extracts the toolbars from the backend
Toolbars::Infos::iterator cit = guiApp->toolbars().begin();
Toolbars::Infos::iterator end = guiApp->toolbars().end();
for (; cit != end; ++cit)
d.toolbars_[cit->name] = new GuiToolbar(*cit, *this);
}
void GuiView::initToolbars()
{
// extracts the toolbars from the backend
Toolbars::Infos::iterator cit = guiApp->toolbars().begin();
Toolbars::Infos::iterator end = guiApp->toolbars().end();
for (; cit != end; ++cit) {
GuiToolbar * tb = toolbar(cit->name);
if (!tb)
continue;
int const visibility = guiApp->toolbars().defaultVisibility(cit->name);
bool newline = !(visibility & Toolbars::SAMEROW);
tb->setVisible(false);
tb->setVisibility(visibility);
if (visibility & Toolbars::TOP) {
if (newline)
addToolBarBreak(Qt::TopToolBarArea);
addToolBar(Qt::TopToolBarArea, tb);
}
if (visibility & Toolbars::BOTTOM) {
// Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
#if (QT_VERSION >= 0x040202)
if (newline)
addToolBarBreak(Qt::BottomToolBarArea);
#endif
addToolBar(Qt::BottomToolBarArea, tb);
}
if (visibility & Toolbars::LEFT) {
// Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
#if (QT_VERSION >= 0x040202)
if (newline)
addToolBarBreak(Qt::LeftToolBarArea);
#endif
addToolBar(Qt::LeftToolBarArea, tb);
}
if (visibility & Toolbars::RIGHT) {
// Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
#if (QT_VERSION >= 0x040202)
if (newline)
addToolBarBreak(Qt::RightToolBarArea);
#endif
addToolBar(Qt::RightToolBarArea, tb);
}
if (visibility & Toolbars::ON)
tb->setVisible(true);
}
}
TocModels & GuiView::tocModels()
{
return d.toc_models_;
}
void GuiView::setFocus()
{
LYXERR(Debug::DEBUG, "GuiView::setFocus()" << this);
QMainWindow::setFocus();
}
void GuiView::focusInEvent(QFocusEvent * e)
{
LYXERR(Debug::DEBUG, "GuiView::focusInEvent()" << this);
QMainWindow::focusInEvent(e);
// Make sure guiApp points to the correct view.
guiApp->setCurrentView(this);
if (currentMainWorkArea())
currentMainWorkArea()->setFocus();
else if (currentWorkArea())
currentWorkArea()->setFocus();
else
d.bg_widget_->setFocus();
}
QMenu * GuiView::createPopupMenu()
{
return d.toolBarPopup(this);
}
void GuiView::showEvent(QShowEvent * e)
{
LYXERR(Debug::GUI, "Passed Geometry "
<< size().height() << "x" << size().width()
<< "+" << pos().x() << "+" << pos().y());
if (d.splitter_->count() == 0)
// No work area, switch to the background widget.
d.setBackground();
QMainWindow::showEvent(e);
}
bool GuiView::closeScheduled()
{
closing_ = true;
return close();
}
/** Destroy only all tabbed WorkAreas. Destruction of other WorkAreas
** is responsibility of the container (e.g., dialog)
**/
void GuiView::closeEvent(QCloseEvent * close_event)
{
LYXERR(Debug::DEBUG, "GuiView::closeEvent()");
if (!GuiViewPrivate::busyBuffers.isEmpty()) {
Alert::warning(_("Exit LyX"), _("LyX could not be closed because documents are processed by LyX."));
close_event->setAccepted(false);
return;
}
// If the user pressed the x (so we didn't call closeView
// programmatically), we want to clear all existing entries.
if (!closing_)
theSession().lastOpened().clear();
closing_ = true;
writeSession();
// it can happen that this event arrives without selecting the view,
// e.g. when clicking the close button on a background window.
setFocus();
if (!closeWorkAreaAll()) {
closing_ = false;
close_event->ignore();
return;
}
// Make sure that nothing will use this to be closed View.
guiApp->unregisterView(this);
if (isFullScreen()) {
// Switch off fullscreen before closing.
toggleFullScreen();
updateDialogs();
}
// Make sure the timer time out will not trigger a statusbar update.
d.statusbar_timer_.stop();
// Saving fullscreen requires additional tweaks in the toolbar code.
// It wouldn't also work under linux natively.
if (lyxrc.allow_geometry_session) {
// Save this window geometry and layout.
saveLayout();
// Then the toolbar private states.
ToolbarMap::iterator end = d.toolbars_.end();
for (ToolbarMap::iterator it = d.toolbars_.begin(); it != end; ++it)
it->second->saveSession();
// Now take care of all other dialogs:
map<string, DialogPtr>::const_iterator it = d.dialogs_.begin();
for (; it!= d.dialogs_.end(); ++it)
it->second->saveSession();
}
close_event->accept();
}
void GuiView::dragEnterEvent(QDragEnterEvent * event)
{
if (event->mimeData()->hasUrls())
event->accept();
/// \todo Ask lyx-devel is this is enough:
/// if (event->mimeData()->hasFormat("text/plain"))
/// event->acceptProposedAction();
}
void GuiView::dropEvent(QDropEvent * event)
{
QList<QUrl> files = event->mimeData()->urls();
if (files.isEmpty())
return;
LYXERR(Debug::GUI, "GuiView::dropEvent: got URLs!");
for (int i = 0; i != files.size(); ++i) {
string const file = os::internal_path(fromqstr(
files.at(i).toLocalFile()));
if (file.empty())
continue;
string const ext = support::getExtension(file);
vector<const Format *> found_formats;
// Find all formats that have the correct extension.
vector<const Format *> const & import_formats
= theConverters().importableFormats();
vector<const Format *>::const_iterator it = import_formats.begin();
for (; it != import_formats.end(); ++it)
if ((*it)->extension() == ext)
found_formats.push_back(*it);
FuncRequest cmd;
if (found_formats.size() >= 1) {
if (found_formats.size() > 1) {
//FIXME: show a dialog to choose the correct importable format
LYXERR(Debug::FILES,
"Multiple importable formats found, selecting first");
}
string const arg = found_formats[0]->name() + " " + file;
cmd = FuncRequest(LFUN_BUFFER_IMPORT, arg);
}
else {
//FIXME: do we have to explicitly check whether it's a lyx file?
LYXERR(Debug::FILES,
"No formats found, trying to open it as a lyx file");
cmd = FuncRequest(LFUN_FILE_OPEN, file);
}
// Asynchronously post the event. DropEvent usually comes
// from the BufferView. But reloading a file might close
// the BufferView from within its own event handler.
guiApp->dispatchDelayed(cmd);
event->accept();
}
}
void GuiView::message(docstring const & str)
{
if (ForkedProcess::iAmAChild())
return;
// call is moved to GUI-thread by GuiProgress
d.progress_->appendMessage(toqstr(str));
}
void GuiView::clearMessageText()
{
message(docstring());
}
void GuiView::updateStatusBarMessage(QString const & str)
{
statusBar()->showMessage(str);
d.statusbar_timer_.stop();
d.statusbar_timer_.start(3000);
}
void GuiView::smallSizedIcons()
{
setIconSize(QSize(d.smallIconSize, d.smallIconSize));
}
void GuiView::normalSizedIcons()
{
setIconSize(QSize(d.normalIconSize, d.normalIconSize));
}
void GuiView::bigSizedIcons()
{
setIconSize(QSize(d.bigIconSize, d.bigIconSize));
}
void GuiView::clearMessage()
{
// FIXME: This code was introduced in r19643 to fix bug #4123. However,
// the hasFocus function mostly returns false, even if the focus is on
// a workarea in this view.
//if (!hasFocus())
// return;
showMessage();
d.statusbar_timer_.stop();
}
void GuiView::updateWindowTitle(GuiWorkArea * wa)
{
if (wa != d.current_work_area_
|| wa->bufferView().buffer().isInternal())
return;
setWindowTitle(qt_("LyX: ") + wa->windowTitle());
setWindowIconText(wa->windowIconText());
}
void GuiView::on_currentWorkAreaChanged(GuiWorkArea * wa)
{
disconnectBuffer();
disconnectBufferView();
connectBufferView(wa->bufferView());
connectBuffer(wa->bufferView().buffer());
d.current_work_area_ = wa;
QObject::connect(wa, SIGNAL(titleChanged(GuiWorkArea *)),
this, SLOT(updateWindowTitle(GuiWorkArea *)));
updateWindowTitle(wa);
structureChanged();
// The document settings needs to be reinitialised.
updateDialog("document", "");
// Buffer-dependent dialogs must be updated. This is done here because
// some dialogs require buffer()->text.
updateDialogs();
}
void GuiView::on_lastWorkAreaRemoved()
{
if (closing_)
// We already are in a close event. Nothing more to do.
return;
if (d.splitter_->count() > 1)
// We have a splitter so don't close anything.
return;
// Reset and updates the dialogs.
d.toc_models_.reset(0);
updateDialog("document", "");
updateDialogs();
resetWindowTitleAndIconText();
updateStatusBar();
if (lyxrc.open_buffers_in_tabs)
// Nothing more to do, the window should stay open.
return;
if (guiApp->viewIds().size() > 1) {
close();
return;
}
#ifdef Q_WS_MACX
// On Mac we also close the last window because the application stay
// resident in memory. On other platforms we don't close the last
// window because this would quit the application.
close();
#endif
}
void GuiView::updateStatusBar()
{
// let the user see the explicit message
if (d.statusbar_timer_.isActive())
return;
showMessage();
}
void GuiView::showMessage()
{
QString msg = toqstr(theGuiApp()->viewStatusMessage());
if (msg.isEmpty()) {
BufferView const * bv = currentBufferView();
if (bv)
msg = toqstr(bv->cursor().currentState());
else
msg = qt_("Welcome to LyX!");
}
statusBar()->showMessage(msg);
}
bool GuiView::event(QEvent * e)
{
switch (e->type())
{
// Useful debug code:
//case QEvent::ActivationChange:
//case QEvent::WindowDeactivate:
//case QEvent::Paint:
//case QEvent::Enter:
//case QEvent::Leave:
//case QEvent::HoverEnter:
//case QEvent::HoverLeave:
//case QEvent::HoverMove:
//case QEvent::StatusTip:
//case QEvent::DragEnter:
//case QEvent::DragLeave:
//case QEvent::Drop:
// break;
case QEvent::WindowActivate: {
GuiView * old_view = guiApp->currentView();
if (this == old_view) {
setFocus();
return QMainWindow::event(e);
}
if (old_view && old_view->currentBufferView()) {
// save current selection to the selection buffer to allow
// middle-button paste in this window.
cap::saveSelection(old_view->currentBufferView()->cursor());
}
guiApp->setCurrentView(this);
if (d.current_work_area_) {
BufferView & bv = d.current_work_area_->bufferView();
connectBufferView(bv);
connectBuffer(bv.buffer());
// The document structure, name and dialogs might have
// changed in another view.
structureChanged();
// The document settings needs to be reinitialised.
updateDialog("document", "");
updateDialogs();
} else {
resetWindowTitleAndIconText();
}
setFocus();
return QMainWindow::event(e);
}
case QEvent::ShortcutOverride: {
// See bug 4888
#if (!defined Q_WS_X11) || (QT_VERSION >= 0x040500)
if (isFullScreen() && menuBar()->isHidden()) {
QKeyEvent * ke = static_cast<QKeyEvent*>(e);
// FIXME: we should also try to detect special LyX shortcut such as
// Alt-P and Alt-M. Right now there is a hack in
// GuiWorkArea::processKeySym() that hides again the menubar for
// those cases.
if (ke->modifiers() & Qt::AltModifier && ke->key() != Qt::Key_Alt) {
menuBar()->show();
return QMainWindow::event(e);