Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

iconboxlabel.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2003 by Bosselut Cyril                                   *
00003  *   bosselut@b1project.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 #include "iconboxlabel.h"
00011 
00012 IconBoxLabel::IconBoxLabel(int ix,int iy,int is,int ia,int io, int iid): Meter(ix,iy,is,is){
00013         X = ix;
00014         Y = iy;
00015         x = X - io;
00016         y = Y - io;
00017         s = is;
00018         w = 0;
00019         h = 0;
00020         if(ia == 1){
00021                 align = ia;
00022         }
00023         else{
00024                 align = 0;
00025         }
00026         roll = io;
00027         id = iid;
00028         iconId = 0;
00029         rightClickOver = 0;
00030 }
00031 
00032 IconBoxLabel::IconBoxLabel() : Meter(){
00033 }
00034 
00035 IconBoxLabel::~IconBoxLabel(){
00036 }
00037 
00038 void IconBoxLabel::readConfig(){
00039         kdDebug("IconBoxLabel") << "(box "<< id << ") Read theme config from: " << themeRC << endl;
00040         KSimpleConfig conf(themeRC);
00041         conf.setGroup(QString("box %1").arg(id));
00042         iconId = conf.readNumEntry("icons",0);
00043         kdDebug("IconBoxLabel") << "(box "<< id << ") " << iconId << " icon(s) in the box" << endl;
00044         if(iconId >= 1){
00045                 int n = 0;
00046                 for(n = 1;n <= iconId;n++){
00047                         QString i = conf.readEntry(QString("icon%1").arg(n), "");
00048                         QString t = conf.readEntry(QString("tip%1").arg(n), "");
00049                         QString p = conf.readEntry(QString("prog%1").arg(n), "");
00050                         addIcon( i, t, p );
00051                 }
00052         }
00053 }
00054 
00055 void IconBoxLabel::selectIcon(){
00056         AddIconDlg* dlg = new AddIconDlg(myKaramba);
00057         dlg->m_icon->setIcon("karamba");
00058         if(dlg->exec()==QDialog::Accepted){
00059                 addIcon( dlg->m_icon->icon(), dlg->m_tip->text(), dlg->m_prog->url() );
00060                 writeConfig(dlg->m_icon->icon(), dlg->m_prog->url(), dlg->m_tip->text());
00061                 //myKaramba->reloadConfig();
00062                 kdDebug("IconBoxLabel") << "(box "<< id << ") " << iconId << " icon(s) in the box" << endl;
00063         }
00064 }
00065 
00066 void IconBoxLabel::writeConfig( const QString icon, const QString tip, const QString prog ){
00067         iconId++;
00068         kdDebug("IconBoxLabel") << "(box "<< id << ") Saving theme config to: " << themeRC << endl;
00069         KSimpleConfig conf(themeRC);
00070         conf.setGroup(QString("box %1").arg(id));
00071         conf.writeEntry("icons", iconId);
00072         conf.writeEntry(QString("icon%1").arg(iconId), icon);
00073         conf.writeEntry(QString("prog%1").arg(iconId), prog);
00074         conf.writeEntry(QString("tip%1").arg(iconId), tip);
00075         conf.sync();
00076 }
00077 
00078 void IconBoxLabel::addIcon( QString icon, QString tip, QString prog ){
00079         if(align == 0){
00080                 x += roll ;
00081                 y = Y;
00082                 w += roll;
00083                 h = roll;
00084                 if(resizable && ((X + w) >= myKaramba->width())){
00085                         kdDebug("IconBoxLabel") << "(box "<< id << ") Previous karamba width: " << myKaramba->width() << endl;
00086                         myKaramba->resize(X + w, myKaramba->height());
00087                         kdDebug("IconBoxLabel") << "(box "<< id << ") New karamba width: " << myKaramba->width() << endl;
00088                 }
00089         }
00090         else if(align == 1){
00091                 x = X;
00092                 y += roll;
00093                 w = roll;
00094                 h += roll;
00095                 if(resizable && (Y + h) >= myKaramba->height()){
00096                         kdDebug("IconBoxLabel") << "(box "<< id << ") Previous karamba height: " << myKaramba->height() << endl;
00097                         myKaramba->resize(myKaramba->width(), Y + h);
00098                         kdDebug("IconBoxLabel") << "(box "<< id << ") New karamba height: " << myKaramba->height() << endl;
00099                 }
00100         }
00101         IconLabel *i = new IconLabel(x, y, roll, roll);
00102         i->setSize( s );
00103         i->setCenter( true, roll, roll );
00104         i->setValue( icon );
00105         i->parseImages(icon, x, y, x, y, roll);
00106         i->setKaramba(myKaramba);
00107         if (!tip.isEmpty()){
00108                 i->setTooltip(tip);
00109         }
00110         myKaramba->meterList->append( i );
00111         myKaramba->iconList->append( i );
00112         ClickArea *c = new ClickArea(x, y, roll, roll);
00113         c->setOnClick( prog );
00114         c->setKaramba(myKaramba);
00115         myKaramba->meterList->append( c );
00116         myKaramba->clickList->append( c );
00117         myKaramba->step();
00118 
00119 }
00120 
00121 void IconBoxLabel::removeIcon(){
00122         if(rightClickOver != 0 && KMessageBox::warningYesNo( myKaramba, tr2i18n("Are you sure that you want to remove icon %1 from box %2?").arg(rightClickOver).arg(id) ) == 3){
00123                 kdDebug("IconBoxLabel") << "(box "<< id << ") Removing icon " << rightClickOver << endl;
00124                 KSimpleConfig conf(themeRC);
00125                 conf.setGroup(QString("box %1").arg(id));
00126                 int icons = conf.readNumEntry("icons",0);
00127                 if(icons >= 1){
00128                         int n = 0;
00129                         QStringList values;
00130                         for(n = 1;n <= icons;n++){
00131                                 if(n != rightClickOver){
00132                                         QString i = conf.readEntry(QString("icon%1").arg(n), "");
00133                                         QString t = conf.readEntry(QString("tip%1").arg(n), "");
00134                                         QString p = conf.readEntry(QString("prog%1").arg(n), "");
00135                                         values << QString("%1\n%2\n%3").arg(i,t,p);
00136                                 }
00137                                 conf.deleteEntry(QString("icon%1").arg(n));
00138                                 conf.deleteEntry(QString("tip%1").arg(n));
00139                                 conf.deleteEntry(QString("prog%1").arg(n));
00140                         }
00141                         iconId = 0;
00142                         conf.sync();
00143                         QStringList::ConstIterator end( values.end() );
00144                         for ( QStringList::ConstIterator it = values.begin(); it != end; ++it ) {
00145                                 writeConfig((*it).section('\n',0,0), (*it).section('\n',1,1), (*it).section('\n',2,2));
00146                         }
00147                         kdDebug("IconBoxLabel") << "(box "<< id << ") " << iconId << " icon(s) left in " << themeRC << endl;
00148                         if(align == 0){
00149                                 x -= roll;
00150                                 w -= roll;
00151                         }
00152                         else if(align == 1){
00153                                 y -= roll;
00154                                 h -= roll;
00155                         }
00156                         myKaramba->reloadConfig();
00157                 }
00158         }
00159         rightClickOver = 0;
00160 }
00161 
00162 void IconBoxLabel::setKaramba(karamba* k){
00163         myKaramba = k;
00164         if(!myKaramba->ksubpop->isItemVisible(id)){
00165                 myKaramba->ksubpop->insertItem( SmallIconSet("fileopen"),tr2i18n("Add an &Icon in box %1").arg(id), this, SLOT(selectIcon()), 0, id );
00166         }
00167         themeRC = myKaramba->themeFile.replace(myKaramba->themePath+"/", "")+"rc";
00168         readConfig();
00169 }
00170 
00171 void IconBoxLabel::setResizable(bool r = false){
00172         resizable = r;
00173 }
00174 
00175 void IconBoxLabel::mUpdate(QPainter* p){
00176         p->setPen(Qt::black);
00177 }
00178 
00179 void IconBoxLabel::mouseOver(QMouseEvent* e){
00180     if (QRect(X, Y, w, h).contains(e->pos())){
00181         if(align == 1){
00182                 for(int i = 1; i <= iconId; i++){
00183                         if (QRect(X, Y + (roll * (i-1)), roll, roll).contains(e->pos())){
00184                                 kdDebug("IconBoxLabel") << "(box "<< id << ") Right click on icon: " << i << endl;
00185                                 rightClickOver = i;
00186                                 if(!myKaramba->ksubpop->isItemVisible(id+100)){
00187                                         myKaramba->ksubpop->insertItem( SmallIconSet("quit"),tr2i18n("Remove this icon"), this, SLOT(removeIcon()), 0, id+100 );
00188                                 }
00189                         }
00190                         
00191                 }
00192         }
00193         else if(align == 0){
00194                 for(int i = 1; i <= iconId; i++){
00195                         if (QRect(X + (roll * (i-1)), Y, roll, roll).contains(e->pos())){
00196                                 kdDebug("IconBoxLabel") << "(box "<< id << ") Right click on icon: " << i << endl;
00197                                 rightClickOver = i;
00198                                 if(!myKaramba->ksubpop->isItemVisible(id+100)){
00199                                         myKaramba->ksubpop->insertItem( SmallIconSet("quit"),tr2i18n("Remove this icon"), this, SLOT(removeIcon()), 0, id+100 );
00200                                 }
00201                         }
00202                         
00203                 }
00204         }
00205     }
00206     else{
00207         rightClickOver = 0;
00208         if(myKaramba->ksubpop->isItemVisible(id+100)){
00209                 myKaramba->ksubpop->removeItem(id+100);
00210         }
00211     }
00212 }
00213 
00214 void IconBoxLabel::dropEvent(QDropEvent *e){
00215         if(e->provides("text/uri-list")){
00216                 e->accept( true );
00217                 e->encodedData("text/uri-list");
00218                 kdDebug("IconBoxLabel") << "(box "<< id << ") text/uri-list drop" << endl;
00219                 QStrList lst;
00220                 QString filename;
00221                 QUriDrag::decode(e, lst);
00222                 for ( uint i = 0; i < lst.count(); ++i ) {
00223                         readDesktopFile(QDir::convertSeparators(QUriDrag::uriToLocalFile(lst.at(i))));
00224                 }
00225         }
00226         else if(e->provides("application/x-qiconlist")){
00227                 kdDebug("IconBoxLabel") << "(box "<< id << ") application/x-qiconlist drop" << endl;
00228                 e->accept( true );
00229                 QStrList lst;
00230                 QString filename;
00231                 QUriDrag::decode(e, lst);
00232                 for ( uint i = 0; i < lst.count(); ++i ) {
00233                         readDesktopFile(QDir::convertSeparators(QUriDrag::uriToLocalFile(lst.at(i))));
00234                 }
00235         }
00236         else{
00237                 e->ignore();
00238                 kdDebug("IconBoxLabel") << "(box "<< id << ") Droped file rejected: " << e->format() << endl;
00239         }
00240 }
00241 
00242 QRect IconBoxLabel::getRectangle()
00243 {
00244     return QRect(X, Y, w, h);
00245 }
00246 
00247 void IconBoxLabel::readDesktopFile(const QString file){
00248         kdDebug("IconBoxLabel") << file << endl;
00249         if(KDesktopFile::isDesktopFile(file)){
00250                 KDesktopFile *fp = new KDesktopFile(file);
00251                 QString type = fp->readType();
00252                 QString name = fp->readName();
00253                 QString icon = fp->readIcon();
00254                 QString app;
00255                 if(type == "Application"){
00256                         app = fp->readEntry("Exec", NULL);
00257                 }
00258                 else if(type = "Link"){
00259                         app = "kfmclient exec " + fp->readURL();
00260                 }
00261                 addIcon(icon, name, app);
00262                 writeConfig(icon, name, app);
00263                 kdDebug("IconBoxLabel") << "(box "<< id << ") " << iconId << " icon(s) in the box" << endl;
00264         }
00265 }
00266 
00267 
00268 

Generated on Mon May 16 13:59:20 2005 for karamba by  doxygen 1.3.9.1