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
|
/*
* Copyright (c) 2009 Cyrille Berger <cberger@cberger.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* either version 2, or (at your option) any later version of the License.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "WebToolWidget.h"
#include <kundo2command.h>
#include <QWebPage>
#include <QWebFrame>
#include "WebShape.h"
#include "WebTool.h"
#include <KoCanvasBase.h>
#include <KoToolManager.h>
#include <KoCanvasController.h>
class ChangeUrl : public KUndo2Command
{
public:
ChangeUrl(WebShape* shape, const QUrl& newUrl) : m_shape(shape), m_newUrl(newUrl), m_oldUrl(shape->url()) {
}
virtual void undo() {
m_shape->setUrl(m_oldUrl);
}
virtual void redo() {
m_shape->setUrl(m_newUrl);
}
private:
WebShape *m_shape;
QUrl m_newUrl;
QUrl m_oldUrl;
};
class ChangeCached : public KUndo2Command
{
public:
ChangeCached(WebShape* shape) : m_shape(shape) {
if(shape->isCached()) {
m_cache = shape->cache();
}
}
virtual void undo() {
m_shape->setCached(!m_shape->isCached());
if(m_shape->isCached()) {
m_shape->setCache(m_cache);
}
}
virtual void redo() {
m_shape->setCached(!m_shape->isCached());
}
private:
WebShape *m_shape;
QString m_cache;
};
WebToolWidget::WebToolWidget(WebTool* _tool) : m_tool(_tool), m_shape(0)
{
m_widget.setupUi(this);
connect(m_widget.urlEdit, SIGNAL(editingFinished()), SLOT(save()));
connect(m_widget.useCache, SIGNAL(stateChanged(int)), SLOT(save()));
connect(m_tool, SIGNAL(shapeChanged(WebShape*)), SLOT(open(WebShape*)));
}
void WebToolWidget::blockChildSignals(bool block)
{
m_widget.urlEdit->blockSignals(block);
m_widget.useCache->blockSignals(block);
}
void WebToolWidget::open(WebShape *shape)
{
m_shape = shape;
if(!m_shape)
return;
blockChildSignals(true);
m_widget.urlEdit->setText(m_shape->url().url());
m_widget.useCache->setChecked(m_shape->isCached());
blockChildSignals(false);
}
void WebToolWidget::save()
{
if(!m_shape)
return;
QString newUrl = m_widget.urlEdit->text();
bool newCached = m_widget.useCache->isChecked();
KoCanvasController* canvasController = KoToolManager::instance()->activeCanvasController();
if(canvasController) {
KoCanvasBase* canvas = canvasController->canvas();
if(newUrl != m_shape->url().url()) {
canvas->addCommand(new ChangeUrl(m_shape, newUrl));
}
if(newCached != m_shape->isCached()) {
canvas->addCommand(new ChangeCached(m_shape));
}
}
}
KUndo2Command * WebToolWidget::createCommand()
{
save();
return 0;
}
WebShape* WebToolWidget::shape()
{
return m_shape;
}
|