there is no moc file generated for this class
[kdegraphics.git] / kolourpaint / environments / document / kpDocumentEnvironment.cpp
blobb1e7bcfb1e3f59d6694d36ec4fdc6595f169ef36
2 /*
3 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #define DEBUG_KP_DOCUMENT_ENVIRONMENT 0
32 #include <kpDocumentEnvironment.h>
34 #include <KDebug>
36 #include <kpMainWindow.h>
37 #include <kpAbstractSelection.h>
38 #include <kpDocument.h>
39 #include <kpEllipticalImageSelection.h>
40 #include <kpFreeFormImageSelection.h>
41 #include <kpImageSelectionTransparency.h>
42 #include <kpRectangularImageSelection.h>
43 #include <kpTextSelection.h>
44 #include <kpTextStyle.h>
45 #if DEBUG_KP_DOCUMENT_ENVIRONMENT
46 #include <kpTool.h>
47 #endif
48 #include <kpViewManager.h>
51 struct kpDocumentEnvironmentPrivate
55 kpDocumentEnvironment::kpDocumentEnvironment (kpMainWindow *mainWindow)
56 : kpEnvironmentBase (mainWindow),
57 d (new kpDocumentEnvironmentPrivate ())
61 kpDocumentEnvironment::~kpDocumentEnvironment ()
63 delete d;
67 // public
68 QWidget *kpDocumentEnvironment::dialogParent () const
70 return mainWindow ();
74 static kpViewManager *ViewManager (kpMainWindow *mw)
76 return mw->viewManager ();
79 // public
80 void kpDocumentEnvironment::setQueueViewUpdates () const
82 ::ViewManager (mainWindow ())->setQueueUpdates ();
85 // public
86 void kpDocumentEnvironment::restoreQueueViewUpdates () const
88 ::ViewManager (mainWindow ())->restoreQueueUpdates ();
92 // public
93 void kpDocumentEnvironment::switchToCompatibleTool (const kpAbstractSelection &selection,
94 bool *isTextChanged) const
96 #if DEBUG_KP_DOCUMENT_ENVIRONMENT
97 kDebug () << "kpDocumentEnvironment::switchToCompatibleTool("
98 << &selection << ")"
99 << " mainwindow.tool="
100 << (mainWindow ()->tool () ? mainWindow ()->tool ()->objectName () : 0)
101 << " mainWindow.toolIsTextTool=" << mainWindow ()->toolIsTextTool ()
102 << " current selection="
103 << document ()->selection ()
104 << " new selection is text="
105 << dynamic_cast <const kpTextSelection *> (&selection)
106 << endl;
107 #endif
109 *isTextChanged = (mainWindow ()->toolIsTextTool () !=
110 (dynamic_cast <const kpTextSelection *> (&selection) != 0));
112 // We don't change the Selection Tool if the new selection's
113 // shape is merely different to the current tool's (e.g. rectangular
114 // vs elliptical) because:
116 // 1. All image selection tools support editing selections of all the
117 // different shapes anyway.
118 // 2. Suppose the user is trying out different drags of selection borders
119 // and then decides to paste a differently shaped selection before continuing
120 // to try out different borders. If the pasting were to switch to
121 // a differently shaped tool, the borders drawn after the paste would
122 // be using a new shape rather than the shape before the paste. This
123 // could get irritating so we don't do the switch.
124 if (!mainWindow ()->toolIsASelectionTool () || *isTextChanged)
126 // See kpDocument::setSelection() APIDoc for this assumption.
127 Q_ASSERT (!document ()->selection ());
129 // Switch to the appropriately shaped selection tool
130 // _before_ we change the selection
131 // (all selection tool's ::end() functions nuke the current selection)
132 if (dynamic_cast <const kpRectangularImageSelection *> (&selection))
134 #if DEBUG_KP_DOCUMENT_ENVIRONMENT
135 kDebug () << "\tswitch to rect selection tool";
136 #endif
137 mainWindow ()->slotToolRectSelection ();
139 else if (dynamic_cast <const kpEllipticalImageSelection *> (&selection))
141 #if DEBUG_KP_DOCUMENT_ENVIRONMENT
142 kDebug () << "\tswitch to elliptical selection tool";
143 #endif
144 mainWindow ()->slotToolEllipticalSelection ();
146 else if (dynamic_cast <const kpFreeFormImageSelection *> (&selection))
148 #if DEBUG_KP_DOCUMENT_ENVIRONMENT
149 kDebug () << "\tswitch to free form selection tool";
150 #endif
151 mainWindow ()->slotToolFreeFormSelection ();
153 else if (dynamic_cast <const kpTextSelection *> (&selection))
155 #if DEBUG_KP_DOCUMENT_ENVIRONMENT
156 kDebug () << "\tswitch to text selection tool";
157 #endif
158 mainWindow ()->slotToolText ();
160 else
161 Q_ASSERT (!"Unknown selection type");
164 #if DEBUG_KP_DOCUMENT_ENVIRONMENT
165 kDebug () << "kpDocumentEnvironment::switchToCompatibleTool(" << &selection << ") finished";
166 #endif
169 // public
170 void kpDocumentEnvironment::assertMatchingUIState (const kpAbstractSelection &selection) const
172 // Trap and try to recover from bugs.
173 // TODO: See kpDocument::setSelection() API comment and determine best fix.
174 const kpAbstractImageSelection *imageSelection =
175 dynamic_cast <const kpAbstractImageSelection *> (&selection);
176 const kpTextSelection *textSelection =
177 dynamic_cast <const kpTextSelection *> (&selection);
178 if (imageSelection)
180 if (imageSelection->transparency () != mainWindow ()->imageSelectionTransparency ())
182 kError () << "kpDocument::setSelection() sel's transparency differs "
183 "from mainWindow's transparency - setting mainWindow's transparency "
184 "to sel"
185 << endl;
186 kError () << "\tisOpaque: sel=" << imageSelection->transparency ().isOpaque ()
187 << " mainWindow=" << mainWindow ()->imageSelectionTransparency ().isOpaque ()
188 << endl;
189 mainWindow ()->setImageSelectionTransparency (imageSelection->transparency ());
192 else if (textSelection)
194 if (textSelection->textStyle () != mainWindow ()->textStyle ())
196 kError () << "kpDocument::setSelection() sel's textStyle differs "
197 "from mainWindow's textStyle - setting mainWindow's textStyle "
198 "to sel"
199 << endl;
200 mainWindow ()->setTextStyle (textSelection->textStyle ());
203 else
205 Q_ASSERT (!"Unknown selection type");
210 #include <kpDocumentEnvironment.moc>