ctkFittedTextBrowser.cpp

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Library:   CTK
00004  
00005   Copyright (c) 2010  Kitware Inc.
00006 
00007   Licensed under the Apache License, Version 2.0 (the "License");
00008   you may not use this file except in compliance with the License.
00009   You may obtain a copy of the License at
00010 
00011       http://www.commontk.org/LICENSE
00012 
00013   Unless required by applicable law or agreed to in writing, software
00014   distributed under the License is distributed on an "AS IS" BASIS,
00015   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016   See the License for the specific language governing permissions and
00017   limitations under the License.
00018  
00019 =========================================================================*/
00020 
00021 // Qt includes
00022 #include <QDebug>
00023 #include <QTextBlock>
00024 #include <QAbstractTextDocumentLayout>
00025 
00026 // CTK includes
00027 #include "ctkFittedTextBrowser.h"
00028 
00029 //-----------------------------------------------------------------------------
00030 ctkFittedTextBrowser::ctkFittedTextBrowser(QWidget* _parent)
00031   : QTextBrowser(_parent)
00032 {
00033   this->connect(this, SIGNAL(textChanged()), SLOT(heightForWidthMayHaveChanged()));
00034 }
00035 
00036 //-----------------------------------------------------------------------------
00037 ctkFittedTextBrowser::~ctkFittedTextBrowser()
00038 {
00039 
00040 }
00041 
00042 //-----------------------------------------------------------------------------
00043 void ctkFittedTextBrowser::heightForWidthMayHaveChanged()
00044 {
00045   this->updateGeometry();
00046 }
00047 
00048 //-----------------------------------------------------------------------------
00049 int ctkFittedTextBrowser::heightForWidth(int _width) const
00050 {
00051   QTextDocument* doc = this->document();
00052   qreal savedWidth = doc->textWidth();
00053   
00054   // Fudge factor. This is the difference between the frame and the 
00055   // viewport.
00056   int fudge = 2 * this->frameWidth();
00057   
00058   // Do the calculation assuming no scrollbars
00059   doc->setTextWidth(_width - fudge);
00060   int noScrollbarHeight =
00061     doc->documentLayout()->documentSize().height() + fudge;
00062   
00063   // (If noScrollbarHeight is greater than the maximum height we'll be
00064   // allowed, then there will be scrollbars, and the actual required
00065   // height will be even higher. But since in this case we've already
00066   // hit the maximum height, it doesn't matter that we underestimate.)
00067   
00068   // Get minimum height (even if string is empty): one line of text
00069   int _minimumHeight = QFontMetrics(doc->defaultFont()).lineSpacing() + fudge;
00070   int ret = qMax(noScrollbarHeight, _minimumHeight);
00071 
00072   doc->setTextWidth(savedWidth);
00073   return ret;
00074 }
00075 
00076 //-----------------------------------------------------------------------------
00077 QSize ctkFittedTextBrowser::minimumSizeHint() const {
00078   QSize s(this->size().width(), 0);
00079   if (s.width() == 0)
00080     {
00081     //s.setWidth(400); // arbitrary value
00082     return QTextBrowser::minimumSizeHint();
00083     }
00084   s.setHeight(this->heightForWidth(s.width()));
00085   return s;
00086 }
00087 
00088 //-----------------------------------------------------------------------------
00089 QSize ctkFittedTextBrowser::sizeHint() const {
00090   return this->minimumSizeHint();
00091 }
00092 
00093 //-----------------------------------------------------------------------------
00094 void ctkFittedTextBrowser::resizeEvent(QResizeEvent* e)
00095 {
00096   this->QTextBrowser::resizeEvent(e);
00097   if (e->size().height() != this->heightForWidth(e->size().width()))
00098     {
00099     this->heightForWidthMayHaveChanged();
00100     }
00101 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated on 21 May 2010 for CTK by  doxygen 1.6.1