F:/KPlato/koffice/libs/kopainter/KoResourceChooser.cpp

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002   Copyright (c) 1999 Carsten Pfeiffer (pfeiffer@kde.org)
00003   Copyright (c) 2002 Igor Jansen (rm@kde.org)
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library 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 GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <QPainter>
00022 #include <QCursor>
00023 #include <QApplication>
00024 #include <QLayout>
00025 #include <QPixmap>
00026 #include <Q3PtrList>
00027 #include <Q3VBoxLayout>
00028 #include <QFrame>
00029 #include <QResizeEvent>
00030 #include <QTableWidget>
00031 #include <QHeaderView>
00032 #include <QTextDocument>
00033 #include <QUrl>
00034 
00035 #include <kglobal.h>
00036 #include <kdebug.h>
00037 
00038 #include "KoResourceChooser.h"
00039 #include "KoItemToolTip.h"
00040 
00041 
00042 QTextDocument *KoIconToolTip::createDocument( const QModelIndex &index )
00043 {
00044     QTextDocument *doc = new QTextDocument( this );
00045 
00046     QImage thumb = index.data( KoResourceChooser::LargeThumbnailRole ).value<QImage>();
00047     doc->addResource( QTextDocument::ImageResource, QUrl( "data:thumbnail" ), thumb );
00048 
00049     QString name = index.data( Qt::DisplayRole ).toString();
00050 
00051     const QString image = QString( "<img src=\"data:thumbnail\">" );
00052     const QString body = QString( "<h3 align=\"center\">%1</h3>" ).arg( name ) + image;
00053     const QString html = QString( "<html><body>%1</body></html>" ).arg( body );
00054 
00055     doc->setHtml( html );
00056     doc->setTextWidth( qMin( doc->size().width(), 500.0 ) );
00057 
00058     return doc;
00059 }
00060 
00061 struct KoResourceChooser::Private
00062 {
00063     int m_itemWidth;
00064     int m_itemHeight;
00065     int m_itemCount;
00066     KoIconToolTip tip;
00067 };
00068 
00069 KoResourceChooser::KoResourceChooser(QSize aIconSize, QWidget *parent)
00070     : QTableWidget(parent)
00071     , d( new Private )
00072 {
00073     d->m_itemCount = 0;
00074     d->m_itemWidth = aIconSize.width();
00075     d->m_itemHeight = aIconSize.height();
00076     setRowCount(1);
00077     setColumnCount(1);
00078     horizontalHeader()->hide();
00079     verticalHeader()->hide();
00080     setSelectionMode(QAbstractItemView::SingleSelection);
00081     installEventFilter(this);
00082 }
00083 
00084 KoResourceChooser::~KoResourceChooser()
00085 {
00086 }
00087 
00088 void KoResourceChooser::keyPressEvent(QKeyEvent * e)
00089 {
00090     if (e->key()== Qt::Key_Return)
00091     {
00092         e->accept();
00093         emit itemClicked(currentItem());
00094     }
00095     else
00096         QTableWidget::keyPressEvent(e);
00097 }
00098 
00099 bool KoResourceChooser::viewportEvent(QEvent *e )
00100 {
00101     if( e->type() == QEvent::ToolTip && model() )
00102     {
00103         QHelpEvent *he = static_cast<QHelpEvent*>(e);
00104         QStyleOptionViewItem option = viewOptions();
00105         QModelIndex index = model()->buddy( indexAt(he->pos()));
00106         option.rect = visualRect( index );
00107         d->tip.showTip( this, he->pos(), option, index );
00108         return true;
00109     }
00110 
00111     return QTableWidget::viewportEvent( e );
00112 }
00113 
00114 // recalculate the number of items that fit into one row
00115 // set the current item again after calculating the new grid
00116 void KoResourceChooser::resizeEvent(QResizeEvent *e)
00117 {
00118     QTableWidget::resizeEvent(e);
00119 
00120     int oldNColumns = columnCount();
00121     int nColumns = width( ) / d->m_itemWidth;
00122     if(nColumns == 0)
00123         nColumns = 1;
00124 
00125     int oldNRows = rowCount();
00126     int nRows = (d->m_itemCount - 1)/ nColumns +1;
00127 
00128     if(nColumns > oldNColumns)
00129     {
00130         // We are now wider than before so we must reorder from the top
00131 
00132         setColumnCount(nColumns); // make sure there is enough space for our reordering
00133         int newRow = 0, newColumn = 0, oldRow = 0, oldColumn = 0;
00134         for(int i = 0; i < d->m_itemCount; i++)
00135         {
00136             QTableWidgetItem *theItem;
00137             theItem = takeItem(oldRow, oldColumn);
00138             setItem(newRow, newColumn, theItem);
00139             newColumn++;
00140             if(newColumn == nColumns)
00141             {
00142                 newColumn = 0;
00143                 newRow++;
00144             }
00145             oldColumn++;
00146             if(oldColumn == oldNColumns)
00147             {
00148                 oldColumn = 0;
00149                 oldRow++;
00150             }
00151         }
00152     }
00153     else if(nColumns < oldNColumns)
00154     {
00155         // We are now not as wide as before so we must reorder from the top
00156 
00157         setRowCount(nRows);// make sure there is enough space for our reordering
00158         int newRow = nRows - 1, newColumn = d->m_itemCount - newRow * nColumns,
00159                       oldRow = oldNRows - 1, oldColumn = d->m_itemCount - oldRow * oldNColumns;
00160         for(int i = 0; i < d->m_itemCount; i++)
00161         {
00162             QTableWidgetItem *theItem;
00163             theItem = takeItem(oldRow, oldColumn);
00164             setItem(newRow, newColumn, theItem);
00165             newColumn--;
00166             if(newColumn < 0)
00167             {
00168                 newColumn = nColumns - 1;
00169                 newRow--;
00170             }
00171             oldColumn--;
00172             if(oldColumn < 0)
00173             {
00174                 oldColumn = oldNColumns - 1;
00175                 oldRow--;
00176             }
00177         }
00178     }
00179 
00180     // Set to the number of rows and columns
00181     setColumnCount(nColumns);
00182     setRowCount(nRows);
00183 
00184     // resize cells in case it's needed
00185     for(int i = 0; i < nColumns; i++)
00186         setColumnWidth(i, d->m_itemWidth);
00187     for(int i = 0; i < nRows; i++)
00188         setRowHeight(i, d->m_itemHeight);
00189 }
00190 
00191 QTableWidgetItem *KoResourceChooser::itemAt(int index)
00192 {
00193     int row = index / columnCount();
00194     int col = index - row * columnCount();
00195     return item(row,col);
00196 }
00197 
00198 // adds an item to the end
00199 void KoResourceChooser::addItem(QTableWidgetItem *item)
00200 {
00201     int row = d->m_itemCount / columnCount();
00202     int col = d->m_itemCount - row* columnCount();
00203     if(row+1 > rowCount())
00204         setRowCount(row+1);
00205     setItem(row, col, item);
00206     d->m_itemCount++;
00207     item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
00208 }
00209 
00210 KoPatternChooser::KoPatternChooser( const Q3PtrList<QTableWidgetItem> &list, QWidget *parent, const char *name )
00211  : QWidget( parent, name )
00212 {
00213     // only serves as beautifier for the iconchooser
00214     //frame = new QHBox( this );
00215     //frame->setFrameStyle( QFrame::Panel | QFrame::Sunken );
00216     chooser = new KoResourceChooser( QSize(30,30), this);
00217 
00218         QObject::connect( chooser, SIGNAL(selected( QTableWidgetItem * ) ),
00219                                                     this, SIGNAL( selected( QTableWidgetItem * )));
00220 
00221         Q3PtrListIterator<QTableWidgetItem> itr( list );
00222         for( itr.toFirst(); itr.current(); ++itr )
00223                 chooser->addItem( itr.current() );
00224 
00225         Q3VBoxLayout *mainLayout = new Q3VBoxLayout( this, 1, -1, "main layout" );
00226         mainLayout->addWidget( chooser, 10 );
00227 }
00228 
00229 
00230 KoPatternChooser::~KoPatternChooser()
00231 {
00232   delete chooser;
00233   //delete frame;
00234 }
00235 
00236 // set the active pattern in the chooser - does NOT emit selected() (should it?)
00237 void KoPatternChooser::setCurrentPattern( QTableWidgetItem *pattern )
00238 {
00239     chooser->setCurrentItem( pattern );
00240 }
00241 
00242 void KoPatternChooser::addPattern( QTableWidgetItem *pattern )
00243 {
00244     chooser->addItem( pattern );
00245 }
00246 
00247 // return the active pattern
00248 QTableWidgetItem *KoPatternChooser::currentPattern()
00249 {
00250     return chooser->currentItem();
00251 }
00252 
00253 #include "KoResourceChooser.moc"

Généré le Wed Nov 22 23:41:06 2006 pour KPlato par  doxygen 1.5.1-p1