1 
2 /*
3    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
4    All rights reserved.
5 
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9 
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.
15 
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.
26 */
27 
28 
29 #define DEBUG_KP_ZOOMED_THUMBNAIL_VIEW 0
30 
31 
32 #include "views/kpZoomedThumbnailView.h"
33 
34 #include "kpLogCategories.h"
35 #include "document/kpDocument.h"
36 #include "views/manager/kpViewManager.h"
37 
38 #include <KLocalizedString>
39 
40 //--------------------------------------------------------------------------------
41 
kpZoomedThumbnailView(kpDocument * document,kpToolToolBar * toolToolBar,kpViewManager * viewManager,kpView * buddyView,kpViewScrollableContainer * scrollableContainer,QWidget * parent)42 kpZoomedThumbnailView::kpZoomedThumbnailView (kpDocument *document,
43         kpToolToolBar *toolToolBar,
44         kpViewManager *viewManager,
45         kpView *buddyView,
46         kpViewScrollableContainer *scrollableContainer,
47         QWidget *parent )
48 
49     : kpThumbnailView (document, toolToolBar, viewManager,
50                        buddyView,
51                        scrollableContainer,
52                        parent)
53 {
54     // Call to virtual function - this is why the class is sealed
55     adjustToEnvironment ();
56 }
57 
58 
59 kpZoomedThumbnailView::~kpZoomedThumbnailView () = default;
60 
61 
62 // public virtual [base kpThumbnailView]
caption() const63 QString kpZoomedThumbnailView::caption () const
64 {
65     return i18n ("%1% - Thumbnail", zoomLevelX ());
66 }
67 
68 
69 // public slot virtual [base kpView]
adjustToEnvironment()70 void kpZoomedThumbnailView::adjustToEnvironment ()
71 {
72 #if DEBUG_KP_ZOOMED_THUMBNAIL_VIEW
73     qCDebug(kpLogViews) << "kpZoomedThumbnailView(" << name ()
74                << ")::adjustToEnvironment()"
75                << " width=" << width ()
76                << " height=" << height ()
77                << endl;
78 #endif
79 
80     if (!document ()) {
81         return;
82     }
83 
84 #if DEBUG_KP_ZOOMED_THUMBNAIL_VIEW
85     qCDebug(kpLogViews) << "\tdoc: width=" << document ()->width ()
86                << " height=" << document ()->height ()
87                << endl;
88 #endif
89 
90     if (document ()->width () <= 0 || document ()->height () <= 0)
91     {
92         qCCritical(kpLogViews) << "kpZoomedThumbnailView::adjustToEnvironment() doc:"
93                    << " width=" << document ()->width ()
94                    << " height=" << document ()->height ();
95         return;
96     }
97 
98 
99     int hzoom = qMax (1, width () * 100 / document ()->width ());
100     int vzoom = qMax (1, height () * 100 / document ()->height ());
101 
102     // keep aspect ratio
103     if (hzoom < vzoom) {
104         vzoom = hzoom;
105     }
106     else {
107         hzoom = vzoom;
108     }
109 
110 #if DEBUG_KP_ZOOMED_THUMBNAIL_VIEW && 1
111     qCDebug(kpLogViews) << "\tproposed zoom=" << hzoom;
112 #endif
113     if (hzoom > 100 || vzoom > 100)
114     {
115     #if DEBUG_KP_ZOOMED_THUMBNAIL_VIEW && 1
116         qCDebug(kpLogViews) << "\twon't magnify - setting zoom to 100%";
117     #endif
118         hzoom = 100;
119         vzoom = 100;
120     }
121 
122 
123     if (viewManager ()) {
124         viewManager ()->setQueueUpdates ();
125     }
126 
127     {
128         setZoomLevel (hzoom, vzoom);
129 
130         setOrigin (QPoint ((width () - zoomedDocWidth ()) / 2,
131                            (height () - zoomedDocHeight ()) / 2));
132         setMaskToCoverDocument ();
133 
134         if (viewManager ()) {
135             viewManager ()->updateView (this);
136         }
137     }
138 
139     if (viewManager ()) {
140         viewManager ()->restoreQueueUpdates ();
141     }
142 }
143 
144 
145