From: Peter Kümmel Date: Tue, 31 Oct 2006 14:12:46 +0000 (+0000) Subject: Add tab support X-Git-Tag: 1.6.10~12125 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=8669b0087e710b72e46573afb3cc46c1d7707e91;p=features.git Add tab support frontends/LyXView.h - add tab update function frontends/qt4/GuiView.h - add tab update function - add function for setting up the tabs - add slot for the tab clicks - add pimpl frontends/qt4/GuiImplementation.C - don't set the buffer as central widget but initilize the tabs frontends/qt4/GuiView.C - use QTabBar for switching - scan the bufferlist on updates frontends/LyXView.C - also update the tabs git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@15637 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/frontends/LyXView.C b/src/frontends/LyXView.C index fe5b702ecd..c32a47e3ce 100644 --- a/src/frontends/LyXView.C +++ b/src/frontends/LyXView.C @@ -131,6 +131,7 @@ void LyXView::setBuffer(Buffer * b) updateLayoutChoice(); updateWindowTitle(); updateStatusBar(); + updateTab(); busy(false); work_area_->redraw(); } @@ -149,6 +150,7 @@ bool LyXView::loadLyXFile(string const & filename, bool tolastfiles) updateToolbars(); updateLayoutChoice(); updateWindowTitle(); + updateTab(); if (loaded) { connectBuffer(*work_area_->bufferView().buffer()); showErrorList("Parse"); diff --git a/src/frontends/LyXView.h b/src/frontends/LyXView.h index f9f7454c44..2624db2486 100644 --- a/src/frontends/LyXView.h +++ b/src/frontends/LyXView.h @@ -153,6 +153,9 @@ public: /// updates the title of the window void updateWindowTitle(); + /// updates the tab view + virtual void updateTab() = 0; + /// reset autosave timer void resetAutosaveTimer(); diff --git a/src/frontends/qt4/GuiImplementation.C b/src/frontends/qt4/GuiImplementation.C index a67d02520f..52eccf5ca8 100644 --- a/src/frontends/qt4/GuiImplementation.C +++ b/src/frontends/qt4/GuiImplementation.C @@ -134,9 +134,10 @@ int GuiImplementation::newWorkArea(unsigned int w, unsigned int h, int view_id) // FIXME BufferView creation should be independant of WorkArea creation buffer_views_[id].reset(new BufferView); work_areas_[id]->setBufferView(buffer_views_[id].get()); - view->setWorkArea(work_areas_[id]); - view->setCentralWidget(work_areas_[id]); + view->setWorkArea(work_areas_[id]); + view->initTab(work_areas_[id]); + work_areas_[id]->setFocus(); return id; } diff --git a/src/frontends/qt4/GuiView.C b/src/frontends/qt4/GuiView.C index d56fced42e..0c110e880b 100644 --- a/src/frontends/qt4/GuiView.C +++ b/src/frontends/qt4/GuiView.C @@ -39,6 +39,8 @@ #include "session.h" #include "lyxfunc.h" #include "MenuBackend.h" +#include "buffer.h" +#include "bufferlist.h" #include #include @@ -54,6 +56,8 @@ using std::endl; using std::string; using std::vector; +using lyx::support::onlyFilename; + namespace lyx { using support::subst; @@ -68,8 +72,38 @@ int const statusbar_timer_value = 3000; } // namespace anon +class WidgetWithTabBar : public QWidget +{ +public: + QTabBar* tabbar; + WidgetWithTabBar(QWidget* w) + { + tabbar = new QTabBar; + QVBoxLayout* l = new QVBoxLayout; + l->addWidget(tabbar); + l->addWidget(w); + l->setMargin(0); + setLayout(l); + } +}; + +struct GuiView::GuiViewPrivate +{ + typedef std::map FuncMap; + typedef std::pair FuncMapPair; + typedef std::map NameMap; + typedef std::pair NameMapPair; + + FuncMap funcmap; + NameMap namemap; + WidgetWithTabBar* wt; + + GuiViewPrivate() + {} +}; + GuiView::GuiView(int id) - : QMainWindow(), LyXView(id), commandbuffer_(0) + : QMainWindow(), LyXView(id), commandbuffer_(0), d(*new GuiViewPrivate) { setAttribute(Qt::WA_DeleteOnClose, true); setAttribute(Qt::WA_QuitOnClose, true); @@ -91,6 +125,7 @@ GuiView::GuiView(int id) GuiView::~GuiView() { + delete &d; } @@ -230,6 +265,123 @@ void GuiView::update_view_state_qt() statusbar_timer_.stop(); } +void GuiView::initTab(QWidget* workarea) +{ + d.wt = new WidgetWithTabBar(workarea); + setCentralWidget(d.wt); + QObject::connect(d.wt->tabbar, SIGNAL(currentChanged(int)), + this, SLOT(currentTabChanged(int))); +} + +void GuiView::updateTab() +{ + QTabBar& tb = *d.wt->tabbar; + + // update when all is done + tb.blockSignals(true); + + typedef std::vector Strings; + Strings const names = theBufferList().getFileNames(); + size_t n_size = names.size(); + + Strings addtab; + // show tabs only when there is more + // than one file opened + if (n_size > 1) + { + for (size_t i = 0; i != n_size; i++) + if (d.namemap.find(names[i]) == d.namemap.end()) + addtab.push_back(names.at(i)); + } + + for(size_t i = 0; i::iterator tabit = d.namemap.begin(); + for (;tabit != d.namemap.end(); ++tabit) + { + bool found = false; + for (size_t i = 0; i != n_size; i++) + if (tabit->first == names.at(i) && notall) + found = true; + if (!found) + removetab.push_back(tabit->first); + } + + + // remove tabs + for(size_t i = 0; isecond) + { + tb.removeTab(i); + break; + } + d.namemap.erase(tabit); + } + } + + // rebuild func map + if (removetab.size() > 0 || addtab.size() > 0) + { + d.funcmap.clear(); + tabit = d.namemap.begin(); + for (;tabit != d.namemap.end(); ++tabit) + { + QTabBar& tb = *d.wt->tabbar; + for (int i = 0; i < tb.count(); i++) + { + if (tb.tabText(i) == tabit->second) + { + FuncRequest func(LFUN_BUFFER_SWITCH, tabit->first); + d.funcmap.insert(GuiViewPrivate::FuncMapPair(i, func)); + break; + } + } + } + } + + // set current tab + if (view()->buffer()) + { + string cur_title = view()->buffer()->fileName(); + if (d.namemap.find(cur_title) != d.namemap.end()) + { + QString tabname = d.namemap.find(cur_title)->second; + for (int i = 0; i < tb.count(); i++) + if (tb.tabText(i) == tabname) + { + tb.setCurrentIndex(i); + break; + } + } + } + + tb.blockSignals(false); + d.wt->update(); +} + +void GuiView::currentTabChanged (int index) +{ + std::map::const_iterator it = d.funcmap.find(index); + if (it != d.funcmap.end()) + activated(it->second); +} + void GuiView::updateStatusBar() { diff --git a/src/frontends/qt4/GuiView.h b/src/frontends/qt4/GuiView.h index f527dcec1d..a1da43712b 100644 --- a/src/frontends/qt4/GuiView.h +++ b/src/frontends/qt4/GuiView.h @@ -26,7 +26,6 @@ #include class QToolBar; - //class FuncRequest; //class string; @@ -70,6 +69,8 @@ public: virtual void clearMessage(); virtual bool hasFocus() const; + virtual void updateTab(); + /// show - display the top-level window void show(); @@ -79,6 +80,7 @@ public: /// menu item has been selected void activated(FuncRequest const &); + void initTab(QWidget* workArea); Q_SIGNALS: void closing(int); @@ -90,6 +92,8 @@ public Q_SLOTS: /// populate a toplevel menu and all its children on demand void updateMenu(QAction *); + void currentTabChanged (int index); + protected: /// make sure we quit cleanly virtual void closeEvent(QCloseEvent * e); @@ -120,6 +124,9 @@ private: void updateFloatingGeometry(); /// QRect floatingGeometry_; + + struct GuiViewPrivate; + GuiViewPrivate& d; }; } // namespace frontend