3 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
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>
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
48 #include <kpViewManager.h>
51 struct kpDocumentEnvironmentPrivate
55 kpDocumentEnvironment::kpDocumentEnvironment (kpMainWindow
*mainWindow
)
56 : kpEnvironmentBase (mainWindow
),
57 d (new kpDocumentEnvironmentPrivate ())
61 kpDocumentEnvironment::~kpDocumentEnvironment ()
68 QWidget
*kpDocumentEnvironment::dialogParent () const
74 static kpViewManager
*ViewManager (kpMainWindow
*mw
)
76 return mw
->viewManager ();
80 void kpDocumentEnvironment::setQueueViewUpdates () const
82 ::ViewManager (mainWindow ())->setQueueUpdates ();
86 void kpDocumentEnvironment::restoreQueueViewUpdates () const
88 ::ViewManager (mainWindow ())->restoreQueueUpdates ();
93 void kpDocumentEnvironment::switchToCompatibleTool (const kpAbstractSelection
&selection
,
94 bool *isTextChanged
) const
96 #if DEBUG_KP_DOCUMENT_ENVIRONMENT
97 kDebug () << "kpDocumentEnvironment::switchToCompatibleTool("
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
)
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";
137 mainWindow ()->slotToolRectSelection ();
139 else if (dynamic_cast <const kpEllipticalImageSelection
*> (&selection
))
141 #if DEBUG_KP_DOCUMENT_ENVIRONMENT
142 kDebug () << "\tswitch to elliptical selection tool";
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";
151 mainWindow ()->slotToolFreeFormSelection ();
153 else if (dynamic_cast <const kpTextSelection
*> (&selection
))
155 #if DEBUG_KP_DOCUMENT_ENVIRONMENT
156 kDebug () << "\tswitch to text selection tool";
158 mainWindow ()->slotToolText ();
161 Q_ASSERT (!"Unknown selection type");
164 #if DEBUG_KP_DOCUMENT_ENVIRONMENT
165 kDebug () << "kpDocumentEnvironment::switchToCompatibleTool(" << &selection
<< ") finished";
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
);
180 if (imageSelection
->transparency () != mainWindow ()->imageSelectionTransparency ())
182 kError () << "kpDocument::setSelection() sel's transparency differs "
183 "from mainWindow's transparency - setting mainWindow's transparency "
186 kError () << "\tisOpaque: sel=" << imageSelection
->transparency ().isOpaque ()
187 << " mainWindow=" << mainWindow ()->imageSelectionTransparency ().isOpaque ()
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 "
200 mainWindow ()->setTextStyle (textSelection
->textStyle ());
205 Q_ASSERT (!"Unknown selection type");
210 #include <kpDocumentEnvironment.moc>