void KateStyleTreeWidget::showEvent( QShowEvent * event ) { QTreeWidget::showEvent(event); resizeColumns(); }
/********************************************************************* * The Tree *********************************************************************/ PrefsTree::PrefsTree( intf_thread_t *_p_intf, QWidget *_parent ) : QTreeWidget( _parent ), p_intf( _p_intf ) { b_show_only_loaded = false; /* General Qt options */ setAlternatingRowColors( true ); setHeaderHidden( true ); setIconSize( QSize( ITEM_HEIGHT,ITEM_HEIGHT ) ); setTextElideMode( Qt::ElideNone ); setUniformRowHeights( true ); CONNECT( this, itemExpanded(QTreeWidgetItem*), this, resizeColumns() ); /* Nice icons */ #define BI( a,b) QIcon a##_icon = QIcon( b ) BI( audio, ":/prefsmenu/advanced/audio" ); BI( video, ":/prefsmenu/advanced/video" ); BI( input, ":/prefsmenu/advanced/codec" ); BI( sout, ":/prefsmenu/advanced/sout" ); BI( advanced, ":/prefsmenu/advanced/extended" ); BI( playlist, ":/prefsmenu/advanced/playlist" ); BI( interface, ":/prefsmenu/advanced/intf" ); #undef BI /* Build the tree for the main module */ module_t *p_module = module_get_main(); /* Initialisation and get the confsize */ PrefsItemData *data = NULL; PrefsItemData *data_sub = NULL; QTreeWidgetItem *current_item = NULL; unsigned confsize; module_config_t *const p_config = module_config_get (p_module, &confsize); /* Go through the list of conf */ for( size_t i = 0; i < confsize; i++ ) { const char *psz_help; QIcon icon; /* Work on a new item */ module_config_t *p_item = p_config + i; switch( p_item->i_type ) { /* This is a category */ case CONFIG_CATEGORY: if( p_item->value.i == -1 ) break; /* PrefsItemData Init */ data = new PrefsItemData( this ); data->name = qtr( config_CategoryNameGet( p_item->value.i ) ); psz_help = config_CategoryHelpGet( p_item->value.i ); if( psz_help ) data->help = qtr( psz_help ); else data->help.clear(); data->i_type = PrefsItemData::TYPE_CATEGORY; data->i_object_id = p_item->value.i; /* This is a category, put a nice icon */ switch( p_item->value.i ) { #define CI(a,b) case a: icon = b##_icon;break CI( CAT_AUDIO, audio ); CI( CAT_VIDEO, video ); CI( CAT_INPUT, input ); CI( CAT_SOUT, sout ); CI( CAT_ADVANCED, advanced ); CI( CAT_PLAYLIST, playlist ); CI( CAT_INTERFACE, interface ); } #undef CI /* Create a new QTreeItem to display it in the tree at top level */ current_item = new QTreeWidgetItem(); current_item->setText( 0, data->name ); current_item->setIcon( 0 , icon ); //current_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) ); current_item->setData( 0, Qt::UserRole, qVariantFromValue( data ) ); addTopLevelItem( current_item ); expandItem( current_item ); break; /* This is a subcategory */ case CONFIG_SUBCATEGORY: if( p_item->value.i == -1 ) break; /* Special cases: move the main subcategories to the parent cat*/ if( data && ( p_item->value.i == SUBCAT_VIDEO_GENERAL || p_item->value.i == SUBCAT_ADVANCED_MISC || p_item->value.i == SUBCAT_INPUT_GENERAL || p_item->value.i == SUBCAT_INTERFACE_GENERAL || p_item->value.i == SUBCAT_SOUT_GENERAL|| p_item->value.i == SUBCAT_PLAYLIST_GENERAL|| p_item->value.i == SUBCAT_AUDIO_GENERAL ) ) { /* Data still contains the correct thing */ data->i_type = PrefsItemData::TYPE_CATSUBCAT; data->i_subcat_id = p_item->value.i; data->name = qtr( config_CategoryNameGet( p_item->value.i ) ); psz_help = config_CategoryHelpGet( p_item->value.i ); if( psz_help ) data->help = qtr( psz_help ); else data->help.clear(); current_item->setData( 0, Qt::UserRole, QVariant::fromValue( data ) ); continue; } /* Normal Subcategories */ /* Process the Data */ data_sub = new PrefsItemData( this ); data_sub->name = qtr( config_CategoryNameGet( p_item->value.i) ); psz_help = config_CategoryHelpGet( p_item->value.i ); if( psz_help ) data_sub->help = qtr( psz_help ); else data_sub->help.clear(); data_sub->i_type = PrefsItemData::TYPE_SUBCATEGORY; data_sub->i_object_id = p_item->value.i; /* Create a new TreeWidget */ QTreeWidgetItem *subcat_item = new QTreeWidgetItem(); subcat_item->setText( 0, data_sub->name ); subcat_item->setData( 0, Qt::UserRole, qVariantFromValue( data_sub ) ); //subcat_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) ); /* Add it to the parent */ assert( current_item ); current_item->addChild( subcat_item ); break; /* Other items don't need yet a place on the tree */ } } module_config_free( p_config ); size_t count; module_t **p_list = module_list_get( &count ); /* Build the tree of plugins */ for( size_t i = 0; i < count; i++ ) { p_module = p_list[i]; // Main module excluded if( module_is_main( p_module) ) continue; unsigned confsize; int i_subcategory = 0, i_category = 0; bool b_options = false; module_config_t *const p_config = module_config_get (p_module, &confsize); /* Loop through the configurations items */ for (size_t i = 0; i < confsize; i++) { const module_config_t *p_item = p_config + i; if( p_item->i_type == CONFIG_CATEGORY ) i_category = p_item->value.i; else if( p_item->i_type == CONFIG_SUBCATEGORY ) i_subcategory = p_item->value.i; if( CONFIG_ITEM(p_item->i_type) ) b_options = true; if( b_options && i_category && i_subcategory ) break; } module_config_free (p_config); /* Dummy item, please proceed */ if( !b_options || i_category == 0 || i_subcategory == 0 ) continue; // Locate the category item; QTreeWidgetItem *subcat_item = NULL; bool b_found = false; for( int i_cat_index = 0 ; i_cat_index < topLevelItemCount(); i_cat_index++ ) { /* Get the treeWidgetItem that correspond to the category */ QTreeWidgetItem *cat_item = topLevelItem( i_cat_index ); PrefsItemData *data = cat_item->data( 0, Qt::UserRole ). value<PrefsItemData *>(); /* If we match the good category */ if( data->i_object_id == i_category ) { for( int i_sc_index = 0; i_sc_index < cat_item->childCount(); i_sc_index++ ) { subcat_item = cat_item->child( i_sc_index ); PrefsItemData *sc_data = subcat_item->data(0, Qt::UserRole). value<PrefsItemData *>(); if( sc_data && sc_data->i_object_id == i_subcategory ) { b_found = true; break; } } if( !b_found ) { subcat_item = cat_item; b_found = true; } break; } } if( !b_found ) continue; PrefsItemData *module_data = new PrefsItemData( this ); module_data->i_type = PrefsItemData::TYPE_MODULE; module_data->psz_shortcut = strdup( module_get_object( p_module ) ); module_data->name = qtr( module_get_name( p_module, false ) ); module_data->help.clear(); const char *psz_help = module_get_help( p_module ); if ( psz_help ) module_data->help = qtr( psz_help ); QTreeWidgetItem *module_item = new QTreeWidgetItem(); module_item->setText( 0, module_data->name ); module_item->setData( 0, Qt::UserRole, QVariant::fromValue( module_data) ); //module_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) ); subcat_item->addChild( module_item ); } /* We got everything, just sort a bit */ sortItems( 0, Qt::AscendingOrder ); module_list_free( p_list ); resizeColumnToContents( 0 ); }
void KateArgumentHintTree::updateGeometry(QRect geom) { //Avoid recursive calls of updateGeometry static bool updatingGeometry = false; if( updatingGeometry ) return; updatingGeometry = true; if( model()->rowCount(QModelIndex()) == 0 ) { /* kDebug( 13035 ) << "KateArgumentHintTree:: empty model";*/ hide(); setGeometry(geom); updatingGeometry = false; return; } int bottom = geom.bottom(); int totalWidth = resizeColumns(); int totalHeight = 0; for(int a = 0; a < model()->rowCount( QModelIndex() ); ++a) { QModelIndex index(model()->index(a, 0)); totalHeight += rowHeight(index); for(int b = 0; b < model()->rowCount(index); ++b) { QModelIndex childIndex = index.child(b, 0); totalHeight += rowHeight(childIndex); } } totalHeight += frameWidth()*2; QRect topRect = visualRect(model()->index(0, 0)); QRect contentRect = visualRect(model()->index(model()->rowCount(QModelIndex())-1, 0)); geom.setHeight(totalHeight); geom.moveBottom(bottom); // if( totalWidth > geom.width() ) geom.setWidth(totalWidth); bool enableScrollBars = false; //Resize and move so it fits the screen horizontally int maxWidth = (QApplication::desktop()->screenGeometry(m_parent->view()).width()*3)/4; if( geom.width() > maxWidth ) { geom.setWidth(maxWidth); geom.setHeight(geom.height() + horizontalScrollBar()->height() +2); geom.moveBottom(bottom); enableScrollBars = true; } if (geom.right() > QApplication::desktop()->screenGeometry(m_parent->view()).right()) geom.moveRight( QApplication::desktop()->screenGeometry(m_parent->view()).right() ); if( geom.left() < QApplication::desktop()->screenGeometry(m_parent->view()).left() ) geom.moveLeft(QApplication::desktop()->screenGeometry(m_parent->view()).left()); //Resize and move so it fits the screen vertically bool resized = false; if( geom.top() < QApplication::desktop()->screenGeometry(this).top() ) { int offset = QApplication::desktop()->screenGeometry(this).top() - geom.top(); geom.setBottom( geom.bottom() - offset ); geom.moveTo(geom.left(), QApplication::desktop()->screenGeometry(this).top()); resized = true; } if(geom != geometry()) { setUpdatesEnabled(false); setAnimated(false); setHorizontalScrollBarPolicy( enableScrollBars ? Qt::ScrollBarAlwaysOn : Qt::ScrollBarAlwaysOff ); /* kDebug( 13035 ) << "KateArgumentHintTree::updateGeometry: updating geometry to " << geom;*/ setGeometry(geom); if( resized && currentIndex().isValid() ) scrollTo(currentIndex()); setUpdatesEnabled(true); } updatingGeometry = false; }
void OperationTable::resizeEvent( QResizeEvent *event ) { QTableWidget::resizeEvent( event ); resizeColumns(); }
void KateCompletionTree::resizeColumns(bool firstShow, bool forceResize) { static bool preventRecursion = false; if (preventRecursion) { return; } m_resizeTimer->stop(); if (firstShow) { forceResize = true; } preventRecursion = true; widget()->setUpdatesEnabled(false); int modelIndexOfName = kateModel()->translateColumn(KTextEditor::CodeCompletionModel::Name); int oldIndentWidth = columnViewportPosition(modelIndexOfName); ///Step 1: Compute the needed column-sizes for the visible content const int numColumns = model()->columnCount(); QVarLengthArray<int, 8> columnSize(numColumns); for (int i = 0; i < numColumns; ++i) { columnSize[i] = 5; } QModelIndex current = indexAt(QPoint(1, 1)); const bool changed = current.isValid(); int currentYPos = 0; measureColumnSizes(this, current, columnSize, currentYPos, height()); int totalColumnsWidth = 0, originalViewportWidth = viewport()->width(); int maxWidth = (QApplication::desktop()->screenGeometry(widget()->view()).width() * 3) / 4; ///Step 2: Update column-sizes //This contains several hacks to reduce the amount of resizing that happens. Generally, //resizes only happen if a) More than a specific amount of space is saved by the resize, or //b) the resizing is required so the list can show all of its contents. int minimumResize = 0; int maximumResize = 0; if (changed) { for (int n = 0; n < numColumns; n++) { totalColumnsWidth += columnSize[n]; int diff = columnSize[n] - columnWidth(n); if (diff < minimumResize) { minimumResize = diff; } if (diff > maximumResize) { maximumResize = diff; } } int noReduceTotalWidth = 0; //The total width of the widget of no columns are reduced for (int n = 0; n < numColumns; n++) { if (columnSize[n] < columnWidth(n)) { noReduceTotalWidth += columnWidth(n); } else { noReduceTotalWidth += columnSize[n]; } } //Check whether we can afford to reduce none of the columns //Only reduce size if we widget would else be too wide. bool noReduce = noReduceTotalWidth < maxWidth && !forceResize; if (noReduce) { totalColumnsWidth = 0; for (int n = 0; n < numColumns; n++) { if (columnSize[n] < columnWidth(n)) { columnSize[n] = columnWidth(n); } totalColumnsWidth += columnSize[n]; } } if (minimumResize > -40 && maximumResize == 0 && !forceResize) { //No column needs to be exanded, and no column needs to be reduced by more than 40 pixels. //To prevent flashing, do not resize at all. totalColumnsWidth = 0; for (int n = 0; n < numColumns; n++) { columnSize[n] = columnWidth(n); totalColumnsWidth += columnSize[n]; } } else { // viewport()->resize( 5000, viewport()->height() ); for (int n = 0; n < numColumns; n++) { setColumnWidth(n, columnSize[n]); } // qCDebug(LOG_KTE) << "resizing viewport to" << totalColumnsWidth; viewport()->resize(totalColumnsWidth, viewport()->height()); } } ///Step 3: Update widget-size and -position int scrollBarWidth = verticalScrollBar()->width(); int newIndentWidth = columnViewportPosition(modelIndexOfName); int newWidth = qMin(maxWidth, qMax(75, totalColumnsWidth)); if (newWidth == maxWidth) { setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); } else { setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); } if (maximumResize > 0 || forceResize || oldIndentWidth != newIndentWidth) { // qCDebug(LOG_KTE) << geometry() << "newWidth" << newWidth << "current width" << width() << "target width" << newWidth + scrollBarWidth; if ((newWidth + scrollBarWidth) != width() && originalViewportWidth != totalColumnsWidth) { widget()->resize(newWidth + scrollBarWidth + 2, widget()->height()); resize(newWidth + scrollBarWidth, widget()->height() - (2 * widget()->frameWidth())); } // qCDebug(LOG_KTE) << "created geometry:" << widget()->geometry() << geometry() << "newWidth" << newWidth << "viewport" << viewport()->width(); if (viewport()->width() > totalColumnsWidth) { //Set the size of the last column to fill the whole rest of the widget setColumnWidth(numColumns - 1, viewport()->width() - columnViewportPosition(numColumns - 1)); } /* for(int a = 0; a < numColumns; ++a) qCDebug(LOG_KTE) << "column" << a << columnWidth(a) << "target:" << columnSize[a];*/ if (oldIndentWidth != newIndentWidth) if (widget()->updatePosition() && !forceResize) { preventRecursion = false; resizeColumns(true, true); } } widget()->setUpdatesEnabled(true); preventRecursion = false; }
void KateCompletionTree::resizeColumnsSlot() { if (model()) { resizeColumns(); } }