libs/flake

KoCopyController.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002  * Copyright (C) 2006-2008 Thomas Zander <zander@kde.org>
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this library; see the file COPYING.LIB.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "KoCopyController.h"
00021 
00022 #include <KoTool.h>
00023 #include <KoCanvasBase.h>
00024 #include <KoToolProxy.h>
00025 #include <KoToolSelection.h>
00026 
00027 #include <KDebug>
00028 #include <QAction>
00029 
00030 // KoCopyControllerPrivate
00031 class KoCopyControllerPrivate
00032 {
00033 public:
00034     KoCopyControllerPrivate(KoCopyController *p, KoCanvasBase *c, QAction *a);
00035 
00036     // request to start the actual copy
00037     void copy();
00038 
00039     // request to start the actual cut
00040     void cut();
00041 
00042     void selectionChanged(bool hasSelection);
00043 
00044     KoCopyController *parent;
00045     KoCanvasBase *canvas;
00046     QAction *action;
00047     bool appHasSelection;
00048 };
00049 
00050 KoCopyControllerPrivate::KoCopyControllerPrivate(KoCopyController *p, KoCanvasBase *c, QAction *a)
00051     : parent(p),
00052     canvas(c),
00053     action(a)
00054 {
00055     appHasSelection = false;
00056 }
00057 
00058 void KoCopyControllerPrivate::copy()
00059 {
00060     if (canvas->toolProxy()->selection() && canvas->toolProxy()->selection()->hasSelection())
00061         // means the copy can be done by a flake tool
00062         canvas->toolProxy()->copy();
00063     else // if not; then the application gets a request to do the copy
00064         emit parent->copyRequested();
00065 }
00066 
00067 void KoCopyControllerPrivate::cut()
00068 {
00069     canvas->toolProxy()->cut();
00070 }
00071 
00072 void KoCopyControllerPrivate::selectionChanged(bool hasSelection)
00073 {
00074     action->setEnabled(appHasSelection || hasSelection);
00075 }
00076 
00077 
00078 // KoCopyController
00079 KoCopyController::KoCopyController(KoCanvasBase *canvas, QAction *copyAction)
00080     : QObject(copyAction),
00081     d(new KoCopyControllerPrivate(this, canvas, copyAction))
00082 {
00083     connect(canvas->toolProxy(), SIGNAL(selectionChanged(bool)), this, SLOT(selectionChanged(bool)));
00084     connect(copyAction, SIGNAL(triggered()), this, SLOT(copy()));
00085     hasSelection(false);
00086 }
00087 
00088 KoCopyController::~KoCopyController()
00089 {
00090     delete d;
00091 }
00092 
00093 void KoCopyController::hasSelection(bool selection)
00094 {
00095     d->appHasSelection = selection;
00096     d->action->setEnabled(d->appHasSelection ||
00097             (d->canvas->toolProxy()->selection() && d->canvas->toolProxy()->selection()->hasSelection()));
00098 }
00099 
00100 #include "KoCopyController.moc"