File indexing completed on 2024-05-05 04:21:19

0001 
0002 /*
0003    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
0004    All rights reserved.
0005 
0006    Redistribution and use in source and binary forms, with or without
0007    modification, are permitted provided that the following conditions
0008    are met:
0009 
0010    1. Redistributions of source code must retain the above copyright
0011       notice, this list of conditions and the following disclaimer.
0012    2. Redistributions in binary form must reproduce the above copyright
0013       notice, this list of conditions and the following disclaimer in the
0014       documentation and/or other materials provided with the distribution.
0015 
0016    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0017    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0018    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0019    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0020    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0021    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0022    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0023    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0025    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026 */
0027 
0028 
0029 #define DEBUG_KP_UNZOOMED_THUMBNAIL_VIEW 0
0030 
0031 
0032 #include "views/kpUnzoomedThumbnailView.h"
0033 
0034 #include "kpLogCategories.h"
0035 #include "document/kpDocument.h"
0036 #include "views/manager/kpViewManager.h"
0037 #include "kpViewScrollableContainer.h"
0038 
0039 #include <QScrollBar>
0040 
0041 #include <KLocalizedString>
0042 
0043 //---------------------------------------------------------------------
0044 
0045 struct kpUnzoomedThumbnailViewPrivate
0046 {
0047 };
0048 
0049 
0050 kpUnzoomedThumbnailView::kpUnzoomedThumbnailView (
0051         kpDocument *document,
0052         kpToolToolBar *toolToolBar,
0053         kpViewManager *viewManager,
0054         kpView *buddyView,
0055         kpViewScrollableContainer *scrollableContainer,
0056         QWidget *parent)
0057 
0058     : kpThumbnailView (document, toolToolBar, viewManager,
0059                        buddyView,
0060                        scrollableContainer,
0061                        parent),
0062       d (new kpUnzoomedThumbnailViewPrivate ())
0063 {
0064     if (buddyViewScrollableContainer ())
0065     {
0066         connect (buddyViewScrollableContainer(), &kpViewScrollableContainer::contentsMoved,
0067                  this, &kpUnzoomedThumbnailView::adjustToEnvironment);
0068     }
0069 
0070     // Call to virtual function - this is why the class is sealed
0071     adjustToEnvironment ();
0072 }
0073 
0074 //---------------------------------------------------------------------
0075 
0076 kpUnzoomedThumbnailView::~kpUnzoomedThumbnailView ()
0077 {
0078     delete d;
0079 }
0080 
0081 //---------------------------------------------------------------------
0082 
0083 // public virtual [base kpThumbnailView]
0084 QString kpUnzoomedThumbnailView::caption () const
0085 {
0086     return i18n ("Unzoomed Mode - Thumbnail");
0087 }
0088 
0089 //---------------------------------------------------------------------
0090 
0091 // public slot virtual [base kpView]
0092 void kpUnzoomedThumbnailView::adjustToEnvironment ()
0093 {
0094     if (!buddyView () || !buddyViewScrollableContainer () || !document ()) {
0095         return;
0096     }
0097 
0098     const int scrollViewContentsX =
0099         buddyViewScrollableContainer()->horizontalScrollBar()->value();
0100     const int scrollViewContentsY =
0101         buddyViewScrollableContainer ()->verticalScrollBar()->value();
0102 
0103 #if DEBUG_KP_UNZOOMED_THUMBNAIL_VIEW
0104     qCDebug(kpLogViews) << "kpUnzoomedThumbnailView(" << name ()
0105                << ")::adjustToEnvironment("
0106                << scrollViewContentsX
0107                << ","
0108                << scrollViewContentsY
0109                << ") width=" << width ()
0110                << " height=" << height ()
0111                << endl;
0112 #endif
0113 
0114 
0115 #if 1
0116     int x;
0117     if (document ()->width () > width ())
0118     {
0119         x = static_cast<int> (buddyView ()->transformViewToDocX (scrollViewContentsX));
0120         const int rightMostAllowedX = qMax (0, document ()->width () - width ());
0121     #if DEBUG_KP_UNZOOMED_THUMBNAIL_VIEW
0122         qCDebug(kpLogViews) << "\tdocX=" << x
0123                 << " docWidth=" << document ()->width ()
0124                 << " rightMostAllowedX=" << rightMostAllowedX;
0125     #endif
0126         if (x > rightMostAllowedX) {
0127             x = rightMostAllowedX;
0128         }
0129     }
0130     // Thumbnail width <= doc width
0131     else
0132     {
0133         // Center X (rather than flush left to be consistent with
0134         //           kpZoomedThumbnailView)
0135         x = -(width () - document ()->width ()) / 2;
0136     }
0137 
0138 
0139     int y;
0140     if (document ()->height () > height ())
0141     {
0142         y = static_cast<int> (buddyView ()->transformViewToDocY (scrollViewContentsY));
0143         const int bottomMostAllowedY = qMax (0, document ()->height () - height ());
0144     #if DEBUG_KP_UNZOOMED_THUMBNAIL_VIEW
0145         qCDebug(kpLogViews) << "\tdocY=" << y
0146                     << " docHeight=" << document ()->height ()
0147                     << " bottomMostAllowedY=" << bottomMostAllowedY;
0148     #endif
0149         if (y > bottomMostAllowedY) {
0150             y = bottomMostAllowedY;
0151         }
0152     }
0153     // Thumbnail height <= doc height
0154     else
0155     {
0156         // Center Y (rather than flush top to be consistent with
0157         //           kpZoomedThumbnailView)
0158         y = -(height () - document ()->height ()) / 2;
0159     }
0160 // Prefer to keep visible area centred in thumbnail instead of flushed left.
0161 // Gives more editing context to the left and top.
0162 // But feels awkward for left-to-right users.  So disabled for now.
0163 // Not totally tested.
0164 #else
0165     if (!buddyViewScrollableContainer ()) {
0166         return;
0167     }
0168 
0169     QRect docRect = buddyView ()->transformViewToDoc (
0170         QRect (buddyViewScrollableContainer ()->horizontalScrollBar()->value(),
0171                buddyViewScrollableContainer ()->verticalScrollBar()->value(),
0172                qMin (buddyView ()->width (), buddyViewScrollableContainer ()->viewport()->width ()),
0173                qMin (buddyView ()->height (), buddyViewScrollableContainer ()->viewport()->height ())));
0174 
0175     x = docRect.x () - (width () - docRect.width ()) / 2;
0176     qCDebug(kpLogViews) << "\tnew suggest x=" << x;
0177     const int rightMostAllowedX = qMax (0, document ()->width () - width ());
0178     if (x < 0) {
0179         x = 0;
0180     }
0181     if (x > rightMostAllowedX) {
0182         x = rightMostAllowedX;
0183     }
0184 
0185     y = docRect.y () - (height () - docRect.height ()) / 2;
0186     qCDebug(kpLogViews) << "\tnew suggest y=" << y;
0187     const int bottomMostAllowedY = qMax (0, document ()->height () - height ());
0188     if (y < 0) {
0189         y = 0;
0190     }
0191     if (y > bottomMostAllowedY) {
0192         y = bottomMostAllowedY;
0193     }
0194 #endif
0195 
0196 
0197     if (viewManager ())
0198     {
0199         viewManager ()->setFastUpdates ();
0200         viewManager ()->setQueueUpdates ();
0201     }
0202 
0203     {
0204         // OPT: scrollView impl would be much, much faster
0205         setOrigin (QPoint (-x, -y));
0206         setMaskToCoverDocument ();
0207 
0208         // Above might be a NOP even if e.g. doc size changed so force
0209         // update
0210         if (viewManager ()) {
0211             viewManager ()->updateView (this);
0212         }
0213     }
0214 
0215     if (viewManager ())
0216     {
0217         viewManager ()->restoreQueueUpdates ();
0218         viewManager ()->restoreFastUpdates ();
0219     }
0220 }
0221 
0222 #include "moc_kpUnzoomedThumbnailView.cpp"