KDevelop API Documentation

filecreate_widget2.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2003 by Julian Rockey                                   *
00003  *   linux@jrockey.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 
00011 #include <qptrlist.h>
00012 #include <qtimer.h>
00013 #include <qwhatsthis.h>
00014 
00015 #include <kparts/part.h>
00016 #include <klibloader.h>
00017 #include <kurl.h>
00018 #include <kdebug.h>
00019 #include <kiconloader.h>
00020 #include <klocale.h>
00021 
00022 #include <kdevcore.h>
00023 
00024 #include "kdevproject.h"
00025 #include "filecreate_part.h"
00026 #include "filecreate_widget2.h"
00027 #include "filecreate_filetype.h"
00028 #include "filecreate_listitem.h"
00029 
00030 namespace FileCreate {
00031 
00032   FriendlyWidget::FriendlyWidget(FileCreatePart *part)
00033     : QTable(0,4,0), TypeChooser(part), m_selected(NULL)
00034   {
00035 
00036     setReadOnly(true);
00037     setShowGrid(false);
00038     horizontalHeader()->hide();
00039     setTopMargin(0);
00040     verticalHeader()->hide();
00041     setLeftMargin(0);
00042     setSelectionMode(SingleRow);
00043     setFocusStyle(FollowStyle);
00044     setColumnStretchable(3, true);
00045 
00046     m_iconLoader = KGlobal::iconLoader();
00047 
00048     QWhatsThis::add(this, i18n("Use this to create new files within your project."));
00049 
00050     setDefaultColumnWidths();
00051 
00052   }
00053 
00054 
00055   FriendlyWidget::~FriendlyWidget()
00056   {
00057   }
00058 
00059   void FriendlyWidget::setCurrent(const FileType * current) {
00060     int changeToRow = -1;
00061     QMap<int,FileType*>::Iterator it;
00062     kdDebug(9034) << "Checking " << current->descr() << " for matches in row..." << endl;
00063     for ( it = typeForRow.begin(); it != typeForRow.end() && changeToRow==-1; ++it ) {
00064       kdDebug(9034) << "Checking: " << it.data()->descr() << endl;
00065       if (it.data()==current)
00066     changeToRow=it.key();
00067       else kdDebug(9034) << "No match!" << endl;
00068     }
00069 
00070     // If an exact match is not found (e.g. current points to a 'parent' type) then
00071     // look instead for an extension match
00072     if (changeToRow==-1) {
00073       for(it = typeForRow.begin(); it!= typeForRow.end() && changeToRow==-1; ++it) {
00074     if (it.data()->ext() == current->ext() )
00075       changeToRow = it.key();
00076       }
00077     }
00078 
00079     if (changeToRow!=-1) {
00080       m_current = current;
00081       kdDebug(9034) << "Found row, setting current to row " << changeToRow << endl;
00082       slotCellSelected(changeToRow,0);
00083       clearSelection();
00084       selectRow(changeToRow);
00085     }
00086 
00087   }
00088 
00089   void FriendlyWidget::refresh() {
00090 
00091     disconnect( this, SIGNAL(currentChanged(int,int)), this, SLOT(slotCellSelected(int,int)) );
00092 
00093     empty();
00094 
00095     int row = 0;
00096     QPtrList<FileType> filetypes = m_part->getFileTypes();
00097     for(FileType * filetype = filetypes.first();
00098     filetype;
00099     filetype=filetypes.next()) {
00100 
00101       if (filetype->enabled()) {
00102 
00103     if (filetype->subtypes().count()==0)
00104       setRow(row++, filetype);
00105 
00106     QPtrList<FileType> subtypes = filetype->subtypes();
00107     for(FileType * subtype = subtypes.first();
00108         subtype;
00109         subtype=subtypes.next()) {
00110       if (subtype->enabled())
00111         setRow(row++, subtype);
00112     }
00113 
00114       }
00115 
00116     }
00117     resizeCells();
00118     if (currentSelection()>-1) removeSelection(currentSelection());
00119 
00120     connect( this, SIGNAL(currentChanged(int,int)), this, SLOT(slotCellSelected(int,int)) );
00121 
00122 
00123   }
00124 
00125   void FriendlyWidget::setRow(int row, FileType * filetype) {
00126     if (row+1>numRows()) setNumRows(row+1);
00127     setText(row, 1, filetype->name() );
00128     setText(row, 2, filetype->ext() );
00129     setText(row, 3, filetype->descr() );
00130     item(row,1)->setWordWrap(true);
00131     item(row,3)->setWordWrap(true);
00132     //setRowStretchable(row,true);
00133     QPixmap iconPix = m_iconLoader->loadIcon(filetype->icon(), KIcon::Desktop, KIcon::SizeMedium,
00134                          KIcon::DefaultState, NULL,
00135                          true);
00136     if (!iconPix.isNull()) {
00137       setPixmap(row, 0, iconPix);
00138       setRowHeight(row, iconPix.height()+4 );
00139       if (iconPix.width()+4>columnWidth(0))
00140     setColumnWidth(0, iconPix.width()+4 );
00141     }
00142 
00143     typeForRow[row]=filetype;
00144 
00145   }
00146 
00147   void FriendlyWidget::empty() {
00148     typeForRow.clear();
00149     while(numRows()) removeRow(0);
00150   }
00151 
00152   void FriendlyWidget::setDefaultColumnWidths() {
00153     // set some defaults - resizeCells will later ensure that column widths
00154     // and row heights are set big enough for the cell contents
00155     setColumnWidth(0,1);
00156     setColumnWidth(1,60);
00157     setColumnWidth(2,30);
00158     setColumnWidth(3,150);
00159   }
00160 
00161   void FriendlyWidget::slotCellSelected(int row, int col) {
00162     if (col!=0) {
00163       setCurrentCell(row, 0);
00164       return;
00165     }
00166 
00167     m_selected = typeForRow.contains(row) ? typeForRow[row] : NULL;
00168     QTimer::singleShot(0, this, SLOT(slotDoSelection()) );
00169 
00170   }
00171 
00172   void FriendlyWidget::slotDoSelection() {
00173     kdDebug(9034) << "widget2: slotDoSelection" << endl;
00174     if (m_selected) filetypeSelected(m_selected);
00175     kdDebug(9034) << "widget2: slotDoSelection middle" << endl;
00176     if (currentSelection()>-1) removeSelection(currentSelection());
00177     kdDebug(9034) << "widget2: slotDoSelection ending" << endl;
00178   }
00179 
00180   void FriendlyWidget::resizeCells() {
00181     for(int r=0;r<numRows();r++) resizeRow(r);
00182     for(int c=0;c<numCols();c++) resizeColumn(c);
00183   }
00184 
00185   void FriendlyWidget::resizeRow(int row) {
00186     if (row>=numRows() || row<0) return;
00187     int maxHeight = 0;
00188 
00189     for(int c=0;c<numCols();c++) {
00190       QTableItem* i = item( row, c );
00191       if( !i )
00192          continue;
00193 
00194       QSize size = i->sizeHint();
00195       maxHeight = size.height()>maxHeight ? size.height() : maxHeight;
00196     }
00197     setRowHeight(row,maxHeight+2); // bit of extra room
00198   }
00199 
00200   void FriendlyWidget::resizeColumn(int col) {
00201     if (col>=numCols() || col<0) return;
00202     int maxWidth = 0;
00203     for(int r=0;r<numRows();r++) {
00204 
00205       QTableItem* i = item( r, col );
00206       if( !i )
00207          continue;
00208 
00209       QSize size = item(r,col)->sizeHint();
00210       maxWidth = size.width()>maxWidth ? size.width() : maxWidth;
00211     }
00212     setColumnWidth(col,maxWidth+2); // bit of extra room
00213   }
00214 
00215 #if QT_VERSION < 0x030100
00216   void FriendlyWidget::selectRow(int row) {
00217     if (numCols()>0 && row<numRows()) {
00218       QTableSelection sel;
00219       sel.init(row,0);
00220       sel.expandTo(row,numCols());
00221       addSelection(sel);
00222     }
00223   }
00224 #endif
00225 
00226 }
00227 
00228 #include "filecreate_widget2.moc"
KDE Logo
This file is part of the documentation for KDevelop Version 3.1.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Feb 22 09:22:40 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003