]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiView.cpp
Let the tag affect other sides too
[lyx.git] / src / frontends / qt4 / GuiView.cpp
index 0b8f69fb3a9e248553732dcc7892c8ea664e0ae7..93099faaf7d5420424ac05cf614c589f32d9cfd9 100644 (file)
 #include "GuiApplication.h"
 #include "GuiCommandBuffer.h"
 #include "GuiCompleter.h"
-#include "GuiWorkArea.h"
 #include "GuiKeySymbol.h"
 #include "GuiToc.h"
 #include "GuiToolbar.h"
+#include "GuiWorkArea.h"
 #include "LayoutBox.h"
 #include "Menus.h"
 #include "TocModel.h"
@@ -154,6 +154,7 @@ public:
                font.setPointSize(int(toqstr(lyxrc.font_sizes[FONT_SIZE_LARGE]).toDouble()));
                pain.setFont(font);
                pain.drawText(260, 15, text);
+               setFocusPolicy(Qt::StrongFocus);
        }
 
        void paintEvent(QPaintEvent *)
@@ -164,6 +165,18 @@ public:
                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_;
 };
@@ -248,6 +261,7 @@ struct GuiView::GuiViewPrivate
        {
                stack_widget_->setCurrentWidget(bg_widget_);
                bg_widget_->setUpdatesEnabled(true);
+               bg_widget_->setFocus();
        }
 
        TabWorkArea * tabWorkArea(int i)
@@ -274,8 +288,11 @@ struct GuiView::GuiViewPrivate
 #if (QT_VERSION >= 0x040400)
        void setPreviewFuture(QFuture<docstring> const & f)
        {
-               if (preview_watcher_.isRunning())
-                       preview_watcher_.waitForFinished();
+               if (preview_watcher_.isRunning()) {
+                       // we prefer to cancel this preview in order to keep a snappy
+                       // interface.
+                       return;
+               }
                preview_watcher_.setFuture(f);
        }
 #endif
@@ -466,6 +483,8 @@ bool GuiView::restoreLayout()
                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();
@@ -517,7 +536,7 @@ void GuiView::initToolbars()
                if (!tb)
                        continue;
                int const visibility = guiApp->toolbars().defaultVisibility(cit->name);
-               bool newline = true;
+               bool newline = !(visibility & Toolbars::SAMEROW);
                tb->setVisible(false);
                tb->setVisibility(visibility);
 
@@ -530,7 +549,8 @@ void GuiView::initToolbars()
                if (visibility & Toolbars::BOTTOM) {
                        // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
 #if (QT_VERSION >= 0x040202)
-                       addToolBarBreak(Qt::BottomToolBarArea);
+                       if (newline)
+                               addToolBarBreak(Qt::BottomToolBarArea);
 #endif
                        addToolBar(Qt::BottomToolBarArea, tb);
                }
@@ -538,7 +558,8 @@ void GuiView::initToolbars()
                if (visibility & Toolbars::LEFT) {
                        // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
 #if (QT_VERSION >= 0x040202)
-                       addToolBarBreak(Qt::LeftToolBarArea);
+                       if (newline)
+                               addToolBarBreak(Qt::LeftToolBarArea);
 #endif
                        addToolBar(Qt::LeftToolBarArea, tb);
                }
@@ -546,7 +567,8 @@ void GuiView::initToolbars()
                if (visibility & Toolbars::RIGHT) {
                        // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
 #if (QT_VERSION >= 0x040202)
-                       addToolBarBreak(Qt::RightToolBarArea);
+                       if (newline)
+                               addToolBarBreak(Qt::RightToolBarArea);
 #endif
                        addToolBar(Qt::RightToolBarArea, tb);
                }
@@ -566,11 +588,22 @@ TocModels & GuiView::tocModels()
 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 LyXFunc points to the correct view.
        guiApp->setCurrentView(this);
-       QMainWindow::setFocus();
-       if (d.current_work_area_)
-               d.current_work_area_->setFocus();
+       if (currentMainWorkArea())
+               currentMainWorkArea()->setFocus();
+       else if (currentWorkArea())
+               currentWorkArea()->setFocus();
+       else
+               d.bg_widget_->setFocus();
 }
 
 
@@ -695,7 +728,7 @@ void GuiView::dropEvent(QDropEvent * event)
                        cmd = FuncRequest(LFUN_FILE_OPEN, file);
                }
 
-               // Asynchronously post the event. DropEvent usually come
+               // 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);
@@ -754,7 +787,8 @@ void GuiView::clearMessage()
 
 void GuiView::updateWindowTitle(GuiWorkArea * wa)
 {
-       if (wa != d.current_work_area_)
+       if (wa != d.current_work_area_
+           || wa->bufferView().buffer().isInternal())
                return;
        setWindowTitle(qt_("LyX: ") + wa->windowTitle());
        setWindowIconText(wa->windowIconText());
@@ -907,25 +941,7 @@ bool GuiView::event(QEvent * e)
                        }
                }
 #endif
-
-               if (d.current_work_area_)
-                       // Nothing special to do.
-                       return QMainWindow::event(e);
-
-               QKeyEvent * ke = static_cast<QKeyEvent*>(e);
-               // Let Qt handle menu access and the Tab keys to navigate keys to navigate
-               // between controls.
-               if (ke->modifiers() & Qt::AltModifier || ke->key() == Qt::Key_Tab 
-                       || ke->key() == Qt::Key_Backtab)
-                       return QMainWindow::event(e);
-
-               // Allow processing of shortcuts that are allowed even when no Buffer
-               // is viewed.
-               KeySymbol sym;
-               setKeySymbol(&sym, ke);
-               guiApp->processKeySym(sym, q_key_state(ke->modifiers()));
-               e->accept();
-               return true;
+               return QMainWindow::event(e);
        }
 
        default:
@@ -1262,6 +1278,7 @@ BufferView const * GuiView::currentBufferView() const
 }
 
 
+#if (QT_VERSION >= 0x040400)
 static docstring saveAndDestroyBuffer(Buffer * buffer, FileName const & fname)
 {
        bool failed = true;
@@ -1279,6 +1296,7 @@ static docstring saveAndDestroyBuffer(Buffer * buffer, FileName const & fname)
                ? _("Automatic save failed!")
                : _("Automatic save done.");
 }
+#endif
 
 
 void GuiView::autoSave()
@@ -2119,6 +2137,7 @@ bool GuiView::closeWorkArea(GuiWorkArea * wa)
 bool GuiView::closeBuffer()
 {
        GuiWorkArea * wa = currentMainWorkArea();
+       setCurrentWorkArea(wa);
        Buffer & buf = wa->bufferView().buffer();
        return wa && closeWorkArea(wa, !buf.parent());
 }
@@ -2307,7 +2326,7 @@ bool GuiView::saveBufferIfNeeded(Buffer & buf, bool hiding)
                buf.removeAutosaveFile();
                if (hiding)
                        // revert all changes
-                       buf.loadLyXFile(buf.fileName());
+                       buf.reload();
                buf.markClean();
                break;
        case 2:
@@ -2404,34 +2423,7 @@ static bool ensureBufferClean(Buffer * buffer)
 void GuiView::reloadBuffer()
 {
        Buffer * buf = &documentBufferView()->buffer();
-       reloadBuffer(buf);
-}
-
-
-void GuiView::reloadBuffer(Buffer * buf)
-{
-       FileName filename = buf->fileName();
-       Buffer const * master = buf->masterBuffer();
-       bool const is_child = master != buf;
-       // The user has already confirmed that the changes, if any, should
-       // be discarded. So we just release the Buffer and don't call closeBuffer();
-       theBufferList().release(buf);
-       buf = loadDocument(filename);
-       docstring const disp_fn = makeDisplayPath(filename.absFilename());
-       docstring str;
-       if (buf) {
-               // re-allocate master if necessary
-               if (is_child && theBufferList().isLoaded(master)
-                   && buf->masterBuffer() != master)
-                       buf->setParent(master);
-               buf->updateLabels();
-               setBuffer(buf);
-               buf->errors("Parse");
-               str = bformat(_("Document %1$s reloaded."), disp_fn);
-       } else {
-               str = bformat(_("Could not reload document %1$s"), disp_fn);
-       }
-       message(str);
+       buf->reload();
 }
 
 
@@ -2448,7 +2440,7 @@ void GuiView::checkExternallyModifiedBuffers()
                        int const ret = Alert::prompt(_("Reload externally changed document?"),
                                                text, 0, 1, _("&Reload"), _("&Cancel"));
                        if (!ret)
-                               reloadBuffer(*bit);
+                               (*bit)->reload();
                }
        }
 }
@@ -2476,7 +2468,8 @@ void GuiView::dispatchVC(FuncRequest const & cmd)
                        break;
                if (buffer->lyxvc().inUse() && !buffer->isReadonly()) {
                        msg = buffer->lyxvc().checkIn();
-                       reloadBuffer();
+                       if (!msg.empty())
+                               reloadBuffer();
                }
                break;
 
@@ -2661,9 +2654,13 @@ bool GuiView::goToFileRow(string const & argument)
 }
 
 
+#if (QT_VERSION >= 0x040400)
 static docstring exportAndDestroy(Buffer * buffer, string const & format)
 {
-       bool const success = buffer->doExport(format, true);
+       bool const update_unincluded =
+                               buffer->params().maintain_unincluded_children
+                               && !buffer->params().getIncludedChildren().empty();
+       bool const success = buffer->doExport(format, true, update_unincluded);
        delete buffer;
        return success
                ? bformat(_("Successful export to format: %1$s"), from_utf8(format))
@@ -2673,12 +2670,16 @@ static docstring exportAndDestroy(Buffer * buffer, string const & format)
 
 static docstring previewAndDestroy(Buffer * buffer, string const & format)
 {
-       bool const success = buffer->preview(format);
+       bool const update_unincluded =
+                               buffer->params().maintain_unincluded_children
+                               && !buffer->params().getIncludedChildren().empty();
+       bool const success = buffer->preview(format, update_unincluded);
        delete buffer;
        return success
                ? bformat(_("Successful preview of format: %1$s"), from_utf8(format))
                : bformat(_("Error previewing format: %1$s"), from_utf8(format));
 }
+#endif
 
 
 bool GuiView::dispatch(FuncRequest const & cmd)
@@ -2731,13 +2732,16 @@ bool GuiView::dispatch(FuncRequest const & cmd)
                        if (argument.empty())
                                format = doc_buffer->getDefaultOutputFormat();
 #if EXPORT_in_THREAD && (QT_VERSION >= 0x040400)
-                       ProgressInterface::instance()->clearMessages();
-                       ProgressInterface::instance()->appendMessage("Exporting ...");
+                       d.progress_->clearMessages();
+                       message(_("Exporting ..."));
                        QFuture<docstring> f = QtConcurrent::run(exportAndDestroy,
                                doc_buffer->clone(), format);
                        d.setPreviewFuture(f);
 #else
-                       doc_buffer->doExport(format, true);
+                       bool const update_unincluded =
+                               doc_buffer->params().maintain_unincluded_children
+                               && !doc_buffer->params().getIncludedChildren().empty();
+                       doc_buffer->doExport(format, true, update_unincluded);
 #endif
                        break;
                }
@@ -2748,13 +2752,16 @@ bool GuiView::dispatch(FuncRequest const & cmd)
                        if (argument.empty())
                                format = doc_buffer->getDefaultOutputFormat();
 #if EXPORT_in_THREAD && (QT_VERSION >= 0x040400)
-                       ProgressInterface::instance()->clearMessages();
-                       ProgressInterface::instance()->appendMessage("Previewing ...");
+                       d.progress_->clearMessages();
+                       message(_("Previewing ..."));
                        QFuture<docstring> f = QtConcurrent::run(previewAndDestroy,
                                doc_buffer->clone(), format);
                        d.setPreviewFuture(f);
 #else
-                       doc_buffer->preview(format);
+                       bool const update_unincluded =
+                               doc_buffer->params().maintain_unincluded_children
+                               && !doc_buffer->params().getIncludedChildren().empty();
+                       doc_buffer->preview(format, update_unincluded);
 #endif
                        break;
                }
@@ -2770,6 +2777,9 @@ bool GuiView::dispatch(FuncRequest const & cmd)
                                master->clone(), format);
                        d.setPreviewFuture(f);
 #else
+                       bool const update_unincluded =
+                               master->params().maintain_unincluded_children
+                               && !master->params().getIncludedChildren().empty();
                        master->doExport(format, true);
 #endif
                        break;
@@ -3369,8 +3379,12 @@ void GuiView::updateDialogs()
 
        for(; it != end; ++it) {
                Dialog * dialog = it->second.get();
-               if (dialog && dialog->isVisibleView())
-                       dialog->checkStatus();
+               if (dialog) {
+                       if (dialog->isBufferDependent() && !documentBufferView())
+                               hideDialog(fromqstr(dialog->name()), 0);
+                       else if (dialog->isVisibleView())
+                               dialog->checkStatus();
+               }
        }
        updateToolbars();
        updateLayoutList();