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
|
/* This file is part of the KDE project
* Copyright (C) 2010 Casper Boemann <cbo@boemann.dk>
* Copyright (C) 2011 Gopalakrishna Bhat A <gopalakbhat@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "SimpleTableOfContentsWidget.h"
#include "ReferencesTool.h"
#include "TableOfContentsConfigure.h"
#include "TableOfContentsTemplate.h"
#include "TableOfContentsPreview.h"
#include <KoTableOfContentsGeneratorInfo.h>
#include <KAction>
#include <KDebug>
#include <QWidget>
#include <QMenu>
#include <QSignalMapper>
SimpleTableOfContentsWidget::SimpleTableOfContentsWidget(ReferencesTool *tool, QWidget *parent)
: QWidget(parent),
m_blockSignals(false),
m_referenceTool(tool),
m_signalMapper(0)
{
widget.setupUi(this);
Q_ASSERT(tool);
m_templateGenerator = new TableOfContentsTemplate(KoTextDocument(m_referenceTool->editor()->document()).styleManager());
widget.addToC->setDefaultAction(tool->action("insert_tableofcontents"));
widget.configureToC->setDefaultAction(tool->action("format_tableofcontents"));
widget.addToC->setNumColumns(1);
connect(widget.addToC, SIGNAL(clicked(bool)), this, SIGNAL(doneWithFocus()));
connect(widget.addToC, SIGNAL(aboutToShowMenu()), this, SLOT(prepareTemplateMenu()));
connect(widget.addToC, SIGNAL(itemTriggered(int)), this, SLOT(applyTemplate(int)));
connect(widget.configureToC, SIGNAL(clicked(bool)), this, SIGNAL(showConfgureOptions()));
}
SimpleTableOfContentsWidget::~SimpleTableOfContentsWidget()
{
delete m_templateGenerator;
}
void SimpleTableOfContentsWidget::setStyleManager(KoStyleManager *sm)
{
m_styleManager = sm;
}
void SimpleTableOfContentsWidget::setToCConfigureMenu(QMenu *tocMenu)
{
if (widget.configureToC->menu()) {
widget.configureToC->menu()->disconnect();
}
widget.configureToC->setMenu(tocMenu);
}
QMenu *SimpleTableOfContentsWidget::ToCConfigureMenu()
{
return widget.configureToC->menu();
}
void SimpleTableOfContentsWidget::showMenu()
{
widget.configureToC->showMenu();
}
void SimpleTableOfContentsWidget::prepareTemplateMenu()
{
m_previewGenerator.clear();
if (m_signalMapper) {
delete m_signalMapper;
m_signalMapper = 0;
}
qDeleteAll(m_templateList.begin(), m_templateList.end());
m_templateList.clear();
m_signalMapper = new QSignalMapper();
m_templateList = m_templateGenerator->templates();
connect(m_signalMapper, SIGNAL(mapped(int)), this, SLOT(pixmapReady(int)));
int index = 0;
foreach (KoTableOfContentsGeneratorInfo *info, m_templateList) {
TableOfContentsPreview *preview = new TableOfContentsPreview();
preview->setStyleManager(KoTextDocument(m_referenceTool->editor()->document()).styleManager());
preview->setPreviewSize(QSize(200,120));
preview->updatePreview(info);
connect(preview, SIGNAL(pixmapGenerated()), m_signalMapper, SLOT(map()));
m_signalMapper->setMapping(preview, index);
m_previewGenerator.append(preview);
++index;
//put dummy pixmaps until the actual pixmap previews are generated and added in pixmapReady()
if (! widget.addToC->hasItemId(index)) {
QPixmap pmm(QSize(200,120));
pmm.fill(Qt::white);
widget.addToC->addItem(pmm, index);
}
}
if (widget.addToC->isFirstTimeMenuShown()) {
widget.addToC->addSeparator();
widget.addToC->addAction(m_referenceTool->action("insert_configure_tableofcontents"));
connect(m_referenceTool->action("insert_configure_tableofcontents"), SIGNAL(triggered()), this, SLOT(insertCustomToC()), Qt::UniqueConnection);
}
}
void SimpleTableOfContentsWidget::pixmapReady(int templateId)
{
// +1 to the templateId is because formattingButton does not allow id = 0
widget.addToC->addItem(m_previewGenerator.at(templateId)->previewPixmap(), templateId + 1);
disconnect(m_previewGenerator.at(templateId), SIGNAL(pixmapGenerated()), m_signalMapper, SLOT(map()));
m_previewGenerator.at(templateId)->deleteLater();
}
void SimpleTableOfContentsWidget::applyTemplate(int templateId)
{
m_templateGenerator->moveTemplateToUsed(m_templateList.at(templateId - 1));
m_referenceTool->editor()->insertTableOfContents(m_templateList.at(templateId - 1));
}
void SimpleTableOfContentsWidget::insertCustomToC()
{
m_templateGenerator->moveTemplateToUsed(m_templateList.at(0));
m_referenceTool->insertCustomToC(m_templateList.at(0));
}
#include <SimpleTableOfContentsWidget.moc>
|