Warning, file /office/calligra/braindump/src/SectionsBoxDock.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *  Copyright (c) 2009 Cyrille Berger <cberger@cberger.net>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation;
0007  * either version 2, or (at your option) any later version of the License.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Lesser General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Lesser General Public License
0015  * along with this library; see the file COPYING.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "SectionsBoxDock.h"
0021 
0022 #include <QMenu>
0023 
0024 #include <KoIcon.h>
0025 
0026 #include "DocumentModel.h"
0027 #include "Section.h"
0028 #include "TreeSortFilter.h"
0029 #include "View.h"
0030 #include "RootSection.h"
0031 #include "Canvas.h"
0032 
0033 #include "commands/InsertSectionCommand.h"
0034 #include "commands/RemoveSectionCommand.h"
0035 
0036 SectionsBoxDock::SectionsBoxDock() : m_view(0), m_model(0), m_proxy(new TreeSortFilter(this))
0037 {
0038     QWidget* mainWidget = new QWidget(this);
0039     setWidget(mainWidget);
0040     setWindowTitle(i18n("Whiteboards"));
0041 
0042     m_wdgSectionsBox.setupUi(mainWidget);
0043 
0044     // Setup list sections
0045     connect(m_wdgSectionsBox.listSections, SIGNAL(clicked(QModelIndex)), SLOT(slotSectionActivated(QModelIndex)));
0046     m_wdgSectionsBox.listSections->setModel(m_proxy);
0047 
0048     // Setup the view mode button
0049     QMenu* m_viewModeMenu = new QMenu(this);
0050     QActionGroup *group = new QActionGroup(this);
0051     QList<QAction*> actions;
0052 
0053     actions << m_viewModeMenu->addAction(koIcon("view-list-text"),
0054                                          i18n("Minimal View"), this, SLOT(slotMinimalView()));
0055     actions << m_viewModeMenu->addAction(koIcon("view-list-details"),
0056                                          i18n("Detailed View"), this, SLOT(slotDetailedView()));
0057     actions << m_viewModeMenu->addAction(koIcon("view-preview"),
0058                                          i18n("Thumbnail View"), this, SLOT(slotThumbnailView()));
0059 
0060     for(int i = 0, n = actions.count(); i < n; ++i) {
0061         actions[i]->setCheckable(true);
0062         actions[i]->setActionGroup(group);
0063     }
0064     actions[1]->trigger(); //TODO save/load previous state
0065 
0066     m_wdgSectionsBox.bnViewMode->setMenu(m_viewModeMenu);
0067     m_wdgSectionsBox.bnViewMode->setPopupMode(QToolButton::InstantPopup);
0068     m_wdgSectionsBox.bnViewMode->setIcon(koIcon("view-choose"));
0069     m_wdgSectionsBox.bnViewMode->setText(i18n("View mode"));
0070 
0071     // Setup the search box
0072     connect(m_wdgSectionsBox.searchLine, SIGNAL(textChanged(QString)), m_proxy, SLOT(setFilterWildcard(QString)));
0073 
0074     // Setup the add button
0075     m_wdgSectionsBox.bnAdd->setIcon(koIcon("list-add"));
0076 
0077     QMenu* newSectionMenu = new QMenu(this);
0078     m_wdgSectionsBox.bnAdd->setMenu(newSectionMenu);
0079     m_wdgSectionsBox.bnAdd->setPopupMode(QToolButton::MenuButtonPopup);
0080     connect(m_wdgSectionsBox.bnAdd, SIGNAL(clicked()), SLOT(slotNewSectionBellowCurrent()));
0081     newSectionMenu->addAction(i18n("Add new whiteboard below current."), this, SLOT(slotNewSectionBellowCurrent()));
0082     newSectionMenu->addAction(i18n("Add new whiteboard above current."), this, SLOT(slotNewSectionAboveCurrent()));
0083     m_newSectionAsChild = newSectionMenu->addAction(i18n("Add new whiteboard as child of current."), this, SLOT(slotNewSectionAsChildOfCurrent()));
0084 
0085     // Setup the delete button
0086     m_wdgSectionsBox.bnDelete->setIcon(koIcon("list-remove"));
0087     connect(m_wdgSectionsBox.bnDelete, SIGNAL(clicked()), SLOT(slotRmClicked()));
0088 
0089     // Setup the raise button
0090     m_wdgSectionsBox.bnRaise->setEnabled(false);
0091     m_wdgSectionsBox.bnRaise->setIcon(koIcon("go-up"));
0092     connect(m_wdgSectionsBox.bnRaise, SIGNAL(clicked()), SLOT(slotRaiseClicked()));
0093 
0094     // Setup the lower button
0095     m_wdgSectionsBox.bnLower->setEnabled(false);
0096     m_wdgSectionsBox.bnLower->setIcon(koIcon("go-down"));
0097     connect(m_wdgSectionsBox.bnLower, SIGNAL(clicked()), SLOT(slotLowerClicked()));
0098 
0099     // Setup the duplicate button
0100     m_wdgSectionsBox.bnDuplicate->setIcon(koIcon("edit-copy"));
0101     connect(m_wdgSectionsBox.bnDuplicate, SIGNAL(clicked()), SLOT(slotDuplicateClicked()));
0102 
0103 }
0104 
0105 SectionsBoxDock::~SectionsBoxDock()
0106 {
0107 }
0108 
0109 void SectionsBoxDock::updateGUI()
0110 {
0111     m_wdgSectionsBox.bnDelete->setEnabled(m_view->activeSection());
0112     m_newSectionAsChild->setEnabled(m_view->activeSection());
0113 }
0114 
0115 void SectionsBoxDock::setup(RootSection* document, View* view)
0116 {
0117     m_view = view;
0118     DocumentModel* model = new DocumentModel(this, document);
0119     m_proxy->setSourceModel(model);
0120     delete m_model;
0121     m_model = model;
0122 
0123     connect(m_model, SIGNAL(activeSectionChanged(Section*)), SLOT(slotSectionActivated(Section*)));
0124     connect(m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), SLOT(insertedSection(QModelIndex,int)));
0125     connect(m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), SLOT(removedSection()));
0126 
0127     updateGUI();
0128 }
0129 
0130 void SectionsBoxDock::slotSectionActivated(const QModelIndex& index)
0131 {
0132     Section* section = qVariantValue<Section*>(m_proxy->data(index, DocumentModel::SectionPtr));
0133     m_view->setActiveSection(section);
0134 }
0135 
0136 void SectionsBoxDock::slotSectionActivated(Section* section)
0137 {
0138     m_view->setActiveSection(section);
0139 }
0140 
0141 void SectionsBoxDock::slotMinimalView()
0142 {
0143     m_wdgSectionsBox.listSections->setDisplayMode(KoDocumentSectionView::MinimalMode);
0144 }
0145 
0146 void SectionsBoxDock::slotDetailedView()
0147 {
0148     m_wdgSectionsBox.listSections->setDisplayMode(KoDocumentSectionView::DetailedMode);
0149 }
0150 
0151 void SectionsBoxDock::slotThumbnailView()
0152 {
0153     m_wdgSectionsBox.listSections->setDisplayMode(KoDocumentSectionView::ThumbnailMode);
0154 }
0155 
0156 void SectionsBoxDock::slotRmClicked()
0157 {
0158     Q_ASSERT(m_view->activeSection());
0159 
0160     m_view->rootSection()->addCommand(m_view->activeSection(), new RemoveSectionCommand(m_view->activeSection(), m_model));
0161 }
0162 void SectionsBoxDock::slotRaiseClicked()
0163 {
0164     qFatal("Unimplemented");
0165 }
0166 
0167 void SectionsBoxDock::slotLowerClicked()
0168 {
0169     qFatal("Unimplemented");
0170 }
0171 
0172 void SectionsBoxDock::slotDuplicateClicked()
0173 {
0174     if(m_view->activeSection()) {
0175         Section* section = new Section(*m_view->activeSection());
0176         m_view->rootSection()->addCommand(section,
0177                                           new InsertSectionCommand(m_view->rootSection()->sectionsIO(), section, m_view->activeSection()->sectionParent(), m_model,
0178                                                   m_view->activeSection()->sectionParent()->nextSection(m_view->activeSection())));
0179     }
0180 }
0181 
0182 void SectionsBoxDock::slotNewSectionAsChildOfCurrent()
0183 {
0184     Q_ASSERT(m_view->activeSection());
0185     Section* section = new Section(m_view->rootSection());
0186     section->setName(SectionGroup::nextName());
0187     m_view->rootSection()->addCommand(section, new InsertSectionCommand(m_view->rootSection()->sectionsIO(), section, m_view->activeSection(), m_model, 0));
0188 }
0189 
0190 void SectionsBoxDock::slotNewSectionAboveCurrent()
0191 {
0192     SectionGroup* parentSection = m_view->activeSection() ? m_view->activeSection()->sectionParent() : m_view->rootSection();
0193     Section* section = new Section(m_view->rootSection());
0194     section->setName(SectionGroup::nextName());
0195     m_view->rootSection()->addCommand(section, new InsertSectionCommand(m_view->rootSection()->sectionsIO(), section, parentSection, m_model, m_view->activeSection()));
0196 }
0197 
0198 void SectionsBoxDock::slotNewSectionBellowCurrent()
0199 {
0200     SectionGroup* parentSection = m_view->activeSection() ? m_view->activeSection()->sectionParent() : m_view->rootSection();
0201     Section* above = parentSection->nextSection(m_view->activeSection());
0202     Section* section = new Section(m_view->rootSection());
0203     section->setName(SectionGroup::nextName());
0204     m_view->rootSection()->addCommand(section, new InsertSectionCommand(m_view->rootSection()->sectionsIO(), section, parentSection, m_model, above));
0205 }
0206 
0207 void SectionsBoxDock::selectSection(Section* section)
0208 {
0209     QModelIndex index = m_proxy->mapFromSource(m_model->index(section));
0210     m_wdgSectionsBox.listSections->setExpanded(index, true);
0211     m_wdgSectionsBox.listSections->setCurrentIndex(index);
0212     slotSectionActivated(index);
0213 }
0214 
0215 void SectionsBoxDock::removedSection()
0216 {
0217     if(m_model->rowCount() == 0) {
0218         m_view->setActiveSection(0);
0219     } else {
0220         slotSectionActivated(m_wdgSectionsBox.listSections->currentIndex());
0221     }
0222 }
0223 
0224 void SectionsBoxDock::insertedSection(const QModelIndex& parent, int idx)
0225 {
0226     QModelIndex index = m_proxy->mapFromSource(m_model->index(idx, 0, parent));
0227     m_wdgSectionsBox.listSections->setExpanded(index, true);
0228     m_wdgSectionsBox.listSections->setCurrentIndex(index);
0229     slotSectionActivated(index);
0230 }