1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
/*
* This file is part of Magellan <http://www.kAlliance.org/Magellan>
*
* Copyright (c) 1998-2000 Teodor Mihai <teddy@ireland.com>
* Copyright (c) 1998-2000 Laur Ivan <laur.ivan@ul.ie>
* Copyright (c) 1999-2000 Virgil Palanciuc <vv@ulise.cs.pub.ro>
*
* Requires the Qt widget libraries, available at no cost at
* http://www.troll.no/
*
* Also requires the KDE libraries, available at no cost at
* http://www.kde.org/
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <etab.h>
#include <qpainter.h>
ETabWidget::ETabWidget(QWidget *parent, const char *name):QWidget(parent, name)
{
}
void ETabWidget::setLabel(const QString &text)
{
label=text;
repaint();
}
void ETabWidget::setPixmap(const QPixmap &pix)
{
pixmap=pix;
repaint();
}
void ETabWidget::paintEvent(QPaintEvent *p)
{
int textXOffset, textYOffset, pixmapXOffset, pixmapYOffset;
// compute offsets
if(pixmap.isNull())
{
textXOffset=TAB_LEFT_SPACING+TAB_SLOPE_WIDTH+TAB_CONTENT_OFFSET;
textYOffset=(height()+fontMetrics().ascent())/2;
}
else
{
pixmapXOffset=TAB_LEFT_SPACING+TAB_SLOPE_WIDTH+TAB_CONTENT_OFFSET;
pixmapYOffset=(height()-pixmap.height())/2;
textXOffset=pixmapXOffset+pixmap.width()+TAB_SPACING;
textYOffset=(height()+fontMetrics().ascent())/2;
}
// erase area
QPainter painter(this);
// painter.eraseRect(0, 0, width(), height());
int bottom;
// draw tab
painter.setPen(colorGroup().dark());
bottom=height()-3;
painter.moveTo(0, bottom);
painter.lineTo(TAB_LEFT_SPACING, bottom);
painter.lineTo(TAB_LEFT_SPACING+TAB_SLOPE_WIDTH, 0);
painter.lineTo(width()-TAB_LEFT_SPACING-TAB_SLOPE_WIDTH, 0);
painter.lineTo(width()-TAB_LEFT_SPACING, bottom);
painter.lineTo(width(), bottom);
// dark margin first
painter.setPen(colorGroup().light());
bottom=height()-2;
painter.moveTo(1, bottom);
painter.lineTo(TAB_LEFT_SPACING, bottom);
painter.lineTo(TAB_LEFT_SPACING+TAB_SLOPE_WIDTH, 1);
painter.lineTo(width()-TAB_LEFT_SPACING-TAB_SLOPE_WIDTH, 1);
painter.lineTo(width()-TAB_LEFT_SPACING, bottom);
painter.lineTo(width(), bottom);
// draw text
painter.setPen(colorGroup().text());
painter.drawText(textXOffset, textYOffset, label);
// draw pixmap, if applicable
if(!pixmap.isNull())
{
painter.drawPixmap(pixmapXOffset, pixmapYOffset, pixmap);
}
}
ETab::ETab(QWidget *parent, const char *name):QWidget(parent, name)
{
lastTabIndex=0;
currentTabIndex=-1;
layout=0;
}
ETab::~ETab()
{
}
int ETab::addTab(QWidget *w, const QString &label, const QPixmap &inactivePixmap, const QPixmap &activePixmap)
{
// avoid crashes if null pointers are passed in here
if(!w)
return -1;
// create a new tab structure
ETabData *tdata=new ETabData;
tabList.append(tdata);
tdata->index=lastTabIndex;
tdata->label=label;
tdata->inactivePixmap=inactivePixmap;
tdata->activePixmap=activePixmap;
tdata->tab=new ETabWidget(this);
tdata->tab->setFixedHeight(TAB_HEIGHT);
tdata->tab->setLabel(label);
tdata->widget=w;
// reparent widget if necessary
if(w->parentWidget()!=this)
w->reparent(this, 0, QPoint(0, 0), false);
// install the event filter
tdata->tab->installEventFilter(this);
// make this tab the current tab, and display everything
currentTabIndex=lastTabIndex++;
rearrange();
return currentTabIndex;
}
int ETab::addTab(QWidget *w, const QString &label)
{
return addTab(w, label, QPixmap(), QPixmap());
}
int ETab::addTab(QWidget *w, const QPixmap &inactivePixmap, const QPixmap &activePixmap)
{
return addTab(w, QString::null, inactivePixmap, activePixmap);
}
bool ETab::removeTab(int index)
{
for(int i=0;i<tabList.count();i++)
{
ETabData *tdata=tabList.at(i);
if(tdata->index==index)
{
delete tdata->widget;
delete tdata->tab;
if(currentTabIndex==index)
currentTabIndex=0;
tabList.remove(i);
delete tdata;
return true;
}
}
return false;
}
bool ETab::removeTab(QWidget *w)
{
for(int i=0;i<tabList.count();i++)
{
ETabData *tdata=tabList.at(i);
if(tdata->widget==w)
{
delete tdata->widget;
delete tdata->tab;
if(currentTabIndex==tdata->index)
currentTabIndex=0;
tabList.remove(i);
delete tdata;
return true;
}
}
return false;
}
int ETab::tabCount()
{
return tabList.count();
}
int ETab::tabIndex(QWidget *w)
{
for(int i=0;i<tabList.count();i++)
{
ETabData *tdata=tabList.at(i);
if(tdata->widget==w)
return tdata->index;
}
return -1;
}
int ETab::currentTab()
{
return currentTabIndex;
}
QWidget *ETab::currentTabWidget()
{
for(int i=0;i<tabList.count();i++)
{
if(tabList.at(i)->index==currentTabIndex)
return tabList.at(i)->widget;
}
return 0;
}
void ETab::activateTab(int tabIndex)
{
if(tabIndex==currentTabIndex) return;
bool found=false;
for(int i=0;i<tabList.count() && !found;i++)
{
if(tabList.at(i)->index==tabIndex)
found=true;
}
if(!found) return;
emit tabSelected(tabIndex, currentTabIndex);
currentTabIndex=tabIndex;
rearrange();
emit tabActivated(tabIndex);
}
void ETab::activateTab(QWidget *widget)
{
activateTab(tabIndex(widget));
}
void ETab::rearrange()
{
if(layout) delete layout;
layout=new QVBoxLayout(this);
for(int i=0;i<tabList.count();i++)
{
ETabData *tdata=tabList.at(i);
layout->addWidget(tdata->tab);
tdata->tab->show();
if(tdata->index==currentTabIndex)
{
layout->addWidget(tdata->widget);
tdata->widget->show();
if(!tdata->activePixmap.isNull())
tdata->tab->setPixmap(tdata->activePixmap);
else if(!tdata->inactivePixmap.isNull())
tdata->tab->setPixmap(tdata->inactivePixmap);
}
else
{
if(!tdata->inactivePixmap.isNull())
tdata->tab->setPixmap(tdata->inactivePixmap);
tdata->widget->hide();
}
}
layout->activate();
}
bool ETab::eventFilter(QObject *o, QEvent *e)
{
for(int i=0;i<tabList.count();i++)
{
if(o==tabList.at(i)->tab && e->type()==QEvent::MouseButtonRelease && tabList.at(i)->index!=currentTabIndex)
{
emit tabSelected(tabList.at(i)->index, currentTabIndex);
currentTabIndex=tabList.at(i)->index;
rearrange();
emit tabActivated(currentTabIndex);
return true;
}
}
return false;
}
|