00001 /* 00002 * The information in this file is 00003 * Copyright(c) 2007 Ball Aerospace & Technologies Corporation 00004 * and is subject to the terms and conditions of the 00005 * GNU Lesser General Public License Version 2.1 00006 * The license text is available from 00007 * http://www.gnu.org/licenses/lgpl.html 00008 */ 00009 00010 #ifndef LABELEDSECTIONGROUP_H 00011 #define LABELEDSECTIONGROUP_H 00012 00013 #include <QtCore/QMap> 00014 #include <QtGui/QLayout> 00015 #include <QtGui/QScrollArea> 00016 00017 #include <vector> 00018 00019 class LabeledSection; 00020 00021 /** 00022 * A widget containing multiple LabeledSection widgets that provides a common 00023 * layout in a scroll area. 00024 * 00025 * This widget subclasses QScrollArea and organizes multiple LabeledSection 00026 * widgets in a QVBoxLayout. Sections are added with optional parameters that 00027 * would normally be passed into a QVBoxLayout when adding child widgets. Each 00028 * section in the layout contains extra spacing to make it easier to visually 00029 * separate the sections. 00030 * 00031 * For size purposes, each LabeledSection can be expanded or collapsed to 00032 * display the entire section widget or just its header label. For adjacent sections, 00033 * call addSection() with a very large stretch value (e.g.: 1000) for each section 00034 * followed by addStretch() with a relatively small value (e.g.: 1) to obtain optimal results. 00035 * 00036 * @see LabeledSection 00037 */ 00038 class LabeledSectionGroup : public QScrollArea 00039 { 00040 Q_OBJECT 00041 00042 public: 00043 /** 00044 * Creates an empty labeled section group. 00045 * 00046 * @param pParent 00047 * The parent widget for the group. 00048 */ 00049 LabeledSectionGroup(QWidget* pParent = NULL); 00050 00051 /** 00052 * Destroys the labeled section group. 00053 * 00054 * When the group is destroyed, all labeled section widgets contained in 00055 * the group are also destroyed. Call clear() prior to destroying the 00056 * group to take ownership of the section widgets. 00057 */ 00058 ~LabeledSectionGroup(); 00059 00060 /** 00061 * Adds a labeled section to the group. 00062 * 00063 * @param pSection 00064 * The labeled section widget to add to the group. This method 00065 * does nothing if \c NULL is passed in. 00066 * @param stretch 00067 * The stretch factor to assign to the added section. The stretch 00068 * factor is passed into QVBoxLayout::addWidget(). 00069 * @param alignment 00070 * The layout alignment to assign to the added section. The 00071 * alignment is passed into QVBoxLayout::addWidget(). 00072 * 00073 * @see addStretch() 00074 */ 00075 void addSection(LabeledSection* pSection, int stretch = 0, Qt::Alignment alignment = 0); 00076 00077 /** 00078 * Adds a stretch item to the end of the layout. 00079 * 00080 * @param stretch 00081 * The stretch factor to add to the layout. The stretch factor is 00082 * passed into QVBoxLayout::addStretch(). 00083 */ 00084 void addStretch(int stretch = 0); 00085 00086 /** 00087 * Removes a labeled section from the group. 00088 * 00089 * This method removes the section from the group but does not delete it. 00090 * Ownership is transferred to the calling object. 00091 * 00092 * @param pSection 00093 * The labeled section widget to remove from the group. This 00094 * method does nothing if \c NULL is passed in or if the given 00095 * section does not exist in the group. 00096 */ 00097 void removeSection(LabeledSection* pSection); 00098 00099 /** 00100 * Queries whether a labeled section exists in the group. 00101 * 00102 * @param pSection 00103 * The labeled section widget to query for its existance in the 00104 * group. 00105 * 00106 * @return Returns \c true if the group contains the given labeled 00107 * section; otherwise returns \c false. 00108 */ 00109 bool hasSection(LabeledSection* pSection) const; 00110 00111 /** 00112 * Returns all labeled sections contained in the group. 00113 * 00114 * @return A vector containing the labeled sections widgets in the group. 00115 */ 00116 std::vector<LabeledSection*> getSections() const; 00117 00118 /** 00119 * Expands a labeled section within the group. 00120 * 00121 * This method visually expands the labeled section by showing its section 00122 * widget. 00123 * 00124 * @param pSection 00125 * The labeled section widget to expand. This method does nothing 00126 * if \c NULL is passed in or if the given section does not exist 00127 * in the group. 00128 * 00129 * @see hasSection(), LabeledSection::expand() 00130 */ 00131 void expandSection(LabeledSection* pSection); 00132 00133 /** 00134 * Collapses a labeled section within the group. 00135 * 00136 * This method visually collapses the labeled section by hiding its section 00137 * widget. 00138 * 00139 * @param pSection 00140 * The labeled section widget to collapse. This method does 00141 * nothing if \c NULL is passed in or if the given section does 00142 * not exist in the group. 00143 * 00144 * @see hasSection(), LabeledSection::collapse() 00145 */ 00146 void collapseSection(LabeledSection* pSection); 00147 00148 /** 00149 * Removes all sections and spacer items from the layout. 00150 * 00151 * This method removes all sections from the group but does not delete 00152 * them. Ownership for all section widgets is transferred to the calling 00153 * object. 00154 */ 00155 void clear(); 00156 00157 /** 00158 * Returns the preferred size of the group. 00159 * 00160 * @return The preferred size of the group that was set by calling 00161 * setSizeHint(). If setSizeHint() has not been called, the value 00162 * returned from the base class QScrollArea::sizeHint() 00163 * implementation is returned. 00164 */ 00165 QSize sizeHint() const; 00166 00167 signals: 00168 /** 00169 * Emitted when one of the contained section widgets is hidden. 00170 * 00171 * This signal is emitted when one of the contained section widgets is 00172 * hidden either programmatically by calling collapseSection() or when the 00173 * user clicks the collapse indicator (-) next to the header text. 00174 * 00175 * @param pSection 00176 * The labeled section that was collapsed. 00177 * 00178 * @see LabeledSection::collapsed() 00179 */ 00180 void sectionCollapsed(LabeledSection* pSection); 00181 00182 /** 00183 * Emitted when one of the contained section widgets is shown. 00184 * 00185 * This signal is emitted when one of the contained section widgets is 00186 * hidden either programmatically by calling expandSection() or when the 00187 * user clicks the expand indicator (+) next to the header text. 00188 * 00189 * @param pSection 00190 * The labeled section that was expanded. 00191 * 00192 * @see LabeledSection::expanded() 00193 */ 00194 void sectionExpanded(LabeledSection* pSection); 00195 00196 protected: 00197 /** 00198 * Sets the preferred size of the group. 00199 * 00200 * This is a convenience method that creates a QSize from the given width 00201 * and height and calls setSizeHint(const QSize&). 00202 * 00203 * @param width 00204 * The preferred group width. 00205 * @param height 00206 * The preferred group height. 00207 */ 00208 void setSizeHint(int width, int height); 00209 00210 /** 00211 * Sets the preferred size of the group. 00212 * 00213 * This method sets a custom preferred size of the group widget that may 00214 * need to be called based on the contents of the labeled sections 00215 * contained in the group. 00216 * 00217 * @param size 00218 * The preferred group size. 00219 * 00220 * @see setSizeHint(int, int) 00221 */ 00222 void setSizeHint(const QSize& size); 00223 00224 private slots: 00225 void enableSectionStretch(); 00226 void disableSectionStretch(); 00227 00228 private: 00229 LabeledSectionGroup(const LabeledSectionGroup& rhs); 00230 LabeledSectionGroup& operator=(const LabeledSectionGroup& rhs); 00231 00232 QVBoxLayout* mpLayout; 00233 QSize mPreferredSize; 00234 QMap<LabeledSection*, int> mSections; 00235 }; 00236 00237 #endif