Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

autoResizeTreeView.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2006 by Graeme Foster                                   *
00003  *   email    foster.graeme@gmail.com                                      *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "autoResizeTreeView.h"
00022 
00023 gf_AutoResizeTreeView::gf_AutoResizeTreeView (QWidget * parent)
00024 :QTreeView(parent)
00025 {
00026         setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
00027         gf_gd = gf_GD_WIDTH;
00028         gf_gp = gf_GP_FRIENDLY;
00029         gf_twin = 0;
00030         gf_friendlyMin = 30;
00031         setMinimumSize(gf_friendlyMin,gf_friendlyMin);
00032         gf_shrinkToWidth = -1;
00033         gf_shrinkToHeight = -1;
00034         gf_hold = true;
00035         setStatus(gf_S_NORMAL);
00036 }
00037 
00038 void gf_AutoResizeTreeView::storeSize()
00039 {
00040         // Capture the current width and height
00041         if (gf_shrinkToWidth == -1)
00042                 gf_shrinkToWidth = width();
00043         if (gf_shrinkToHeight == -1)
00044                 gf_shrinkToHeight = height();
00045 }
00046 
00047 void gf_AutoResizeTreeView::restoreSize()
00048 {
00049         // Restore back to the stored values
00050         if (gf_shrinkToWidth != -1 && gf_shrinkToHeight != -1)
00051         {
00052                 setMaximumSize(1000,1000);
00053                 resize(gf_shrinkToWidth,gf_shrinkToHeight);
00054         }
00055 }
00056 
00057 void gf_AutoResizeTreeView::resizeEvent(QResizeEvent * event)
00058 {
00059         QTreeView::resizeEvent(event);
00060         switch (status)
00061         {
00062                 case gf_S_GROWING:
00063                         status = gf_S_GROWN;
00064                         break;
00065                 case gf_S_SHRINKING:
00066                         status = gf_S_SHRUNK;
00067                         break;
00068                 case gf_S_SHRUNK:
00069                         if (gf_gd & gf_GD_WIDTH)
00070                                 gf_twin->growWidth(event->size().width() - event->oldSize().width());
00071                         if (gf_gd & gf_GD_HEIGHT)
00072                                 gf_twin->growHeight(event->size().height() - event->oldSize().height());
00073                         break;
00074                 case gf_S_NORMAL:
00075                 case gf_S_GROWN:
00076                         if (gf_shrinkToWidth != -1 && gf_shrinkToHeight != -1)
00077                         {
00078                                 gf_shrinkToWidth  += (event->size().width() - event->oldSize().width());
00079                                 gf_shrinkToHeight += (event->size().height() - event->oldSize().height());
00080                         }
00081         } // end of the switch on the status.
00082 //      QTreeView::resizeEvent(event);
00083 }
00084 
00085 void gf_AutoResizeTreeView::focusInEvent ( QFocusEvent * event )
00086 {
00087         QTreeView::focusInEvent(event);
00088         if (gf_twin != 0)
00089         {
00090                 // Occasionally the focusOutEvent doesn't get triggered so
00091                 // If necessary shrink back to the original size
00092                 setStatus(gf_S_GROWING);
00093                 gf_twin->setStatus(gf_S_SHRINKING);
00094                 shrink();
00095                 
00096                 storeSize();
00097                 gf_twin->storeSize();
00098                 // Now select the appropriate grow method
00099                 setStatus(gf_S_GROWING);
00100                 gf_twin->setStatus(gf_S_SHRINKING);
00101                 gf_twin->clearSelection();
00102                 selectGrow();
00103         }
00104 //      QTreeView::focusInEvent(event);
00105 }
00106 
00107 void gf_AutoResizeTreeView::focusOutEvent ( QFocusEvent * event )
00108 {
00109         QTreeView::focusOutEvent(event);
00110         if (gf_hold==false && gf_twin != 0)
00111         {
00112                 setStatus(gf_S_NORMAL);
00113                 gf_twin->setStatus(gf_S_NORMAL);
00114                 shrink();
00115         }
00116 //      QTreeView::focusOutEvent(event);
00117 }
00118 
00119 void gf_AutoResizeTreeView::selectGrow()
00120 {
00121         if (gf_twin != 0)
00122         {
00123                 switch (gf_gp)
00124                 {
00125                         case gf_GP_GREEDY:
00126                                 grow(1,1);
00127                                 break;
00128                         case gf_GP_FRIENDLY:
00129                                 grow(gf_friendlyMin,gf_friendlyMin);
00130                                 break;
00131                         case gf_GP_STAGGERED:
00132                                 staggeredGrow();
00133                                 break;
00134                         case gf_GP_NONE:
00135                                 // Do nothing
00136                                 break;
00137                 } //  end of select on the grow policy
00138         } // end of if a twin exists.
00139 } // end of method selectGrow
00140 
00141 void gf_AutoResizeTreeView::grow(int smallWidth, int smallHeight)
00142 {
00143         if (gf_gd & gf_GD_WIDTH)
00144         {
00145                 int gf_twinwidth = gf_twin->width();
00146                 gf_twin->setMaximumWidth(smallWidth);
00147                 resize(width() +gf_twinwidth - smallWidth,height());
00148         }
00149         
00150         if (gf_gd & gf_GD_HEIGHT)
00151         {
00152                 int gf_twinheight = gf_twin->height();
00153                 gf_twin->setMaximumHeight(smallHeight);
00154                 resize(width(),height() +gf_twinheight - smallHeight);
00155         }
00156 }
00157 
00158 void gf_AutoResizeTreeView::staggeredGrow()
00159 {
00160 }
00161 
00162 void gf_AutoResizeTreeView::shrink()
00163 {
00164         int totalWidth  = gf_twin->gf_shrinkToWidth + gf_shrinkToWidth;
00165         int totalHeight = gf_twin->gf_shrinkToHeight + gf_shrinkToHeight;
00166         // Calculate the new sizes
00167         gf_twin->gf_shrinkToWidth = totalWidth / 2;
00168         gf_shrinkToWidth  = totalWidth - gf_twin->gf_shrinkToWidth;
00169         gf_twin->gf_shrinkToHeight = totalHeight / 2;
00170         gf_shrinkToHeight = totalHeight - gf_twin->gf_shrinkToHeight;
00171         // Restore the sizes.
00172         restoreSize();
00173         gf_twin->restoreSize();
00174 }

Generated on Thu Apr 6 16:27:17 2006 for time-table by  doxygen 1.4.4