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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
|
#include "titledpanel.h"
#include "utilsSystem.h"
Q_DECLARE_METATYPE(QAction *)
QHash<QString, TitledPanelPage *> TitledPanelPage::allPages;
TitledPanelPage::TitledPanelPage(QWidget *widget, const QString &id, const QString &text, const QIcon &icon) :
m_widget(0), m_visibleAction(0), m_selectAction(0), m_toolbarActions(0)
{
#ifndef QT_NO_DEBUG
if (allPages.contains(id)) {
qDebug() << "Duplicate TitledPanelPage ID:" << id;
}
if (!widget) qDebug() << "Page does not have widget!" << id;
#endif
allPages.insert(id, this);
m_widget = widget;
m_widget->setProperty("containingPage", QVariant::fromValue<TitledPanelPage *>(this));
QFrame *f = qobject_cast<QFrame *>(m_widget); // remove frame form widget if it has one
if (f) f->setFrameShape(QFrame::NoFrame);
m_id = id;
m_title = text;
m_icon = icon;
m_visibleAction = new QAction(text, this);
m_visibleAction->setCheckable(true);
m_visibleAction->setChecked(true);
m_visibleAction->setData(id);
// select action
// TODO check
m_selectAction = new QAction(text, this);
m_selectAction->setData(id);
m_selectAction->setIcon(icon);
m_selectAction->setToolTip(text);
m_selectAction->setCheckable(true);
m_toolbarActions = new QList<QAction *>();
}
TitledPanelPage::~TitledPanelPage()
{
allPages.remove(m_id);
delete m_toolbarActions; // only deletes the list. The actions themselves are not owned by the page
}
void TitledPanelPage::addToolbarAction(QAction *act)
{
m_toolbarActions->append(act);
}
void TitledPanelPage::addToolbarActions(QList<QAction *> actions)
{
m_toolbarActions->append(actions);
}
QString TitledPanelPage::id() const
{
return m_id;
}
QWidget *TitledPanelPage::widget()
{
return m_widget;
}
bool TitledPanelPage::visible() const
{
return m_visibleAction->isChecked();
}
TitledPanelPage *TitledPanelPage::fromId(const QString &id)
{
TitledPanelPage *page = allPages.value(id);
if (!page) qDebug() << "Requested TitledPanelPage does not exist:" << id;
return page;
}
void TitledPanelPage::updatePageTitle(const QString &id, const QString newTitle)
{
TitledPanelPage *page = allPages.value(id);
if (page) page->setTitle(newTitle);
}
void TitledPanelPage::setTitle(const QString &title)
{
if (m_title != title) {
m_title = title;
m_visibleAction->setText(title);
m_selectAction->setText(title);
m_selectAction->setToolTip(title);
emit titleChanged(title);
}
}
void TitledPanelPage::setIcon(const QIcon &icon)
{
m_icon = icon;
m_selectAction->setIcon(icon);
emit iconChanged(icon);
}
/*** class TitledPanel ***/
TitledPanel::TitledPanel(QWidget *parent) :
QFrame(parent), mToggleViewAction(0), closeAction(0), pageSelectActions(0), selectorStyle(ComboSelector), vLayout(0),
topbar(0), lbTopbarLabel(0), cbTopbarSelector(0), tbTopbarSelector(0), stack(0)
{
setFrameShape(QFrame::Box);
setFrameShadow(QFrame::Plain);
mToggleViewAction = new QAction(this);
mToggleViewAction->setCheckable(true);
mToggleViewAction->setChecked(true);
connect(mToggleViewAction, SIGNAL(toggled(bool)), this, SLOT(viewToggled(bool)));
pageSelectActions = new QActionGroup(this);
closeAction = new QAction(this);
closeAction->setIcon(getRealIcon("close"));
closeAction->setToolTip(tr("Close"));
connect(closeAction, SIGNAL(triggered()), this, SLOT(hide()));
stack = new QStackedWidget();
vLayout = new QVBoxLayout(this);
vLayout->setSpacing(0);
vLayout->setMargin(0);
vLayout->addWidget(stack);
setLayout(vLayout);
updateTopbar();
}
/* note: the panel takes ownership of the page */
void TitledPanel::appendPage(TitledPanelPage *page, bool guiUpdate)
{
page->setParent(this);
pages.append(page);
stack->addWidget(page->m_widget);
page->m_widget->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Expanding);
// TODO adapt for horizontal or vertical panels
page->m_widget->setMinimumWidth(0);
connect(page, SIGNAL(titleChanged(QString)), this, SLOT(onPageTitleChange()));
connect(page, SIGNAL(iconChanged(QIcon)), this, SLOT(onPageIconChange()));
connect(page->m_selectAction, SIGNAL(toggled(bool)), this, SLOT(setActivePageFromAction()));
connect(page->m_visibleAction, SIGNAL(toggled(bool)), this, SLOT(togglePageVisibleFromAction(bool)));
if (guiUpdate) updateTopbar();
}
/* note: the panel doesn't have a parent anymore. You are responsible for deleting */
void TitledPanel::removePage(TitledPanelPage *page, bool guiUpdate)
{
disconnect(page, SIGNAL(titleChanged()), this, SLOT(onPageTitleChange()));
disconnect(page, SIGNAL(iconChanged()), this, SLOT(onPageIconChange()));
disconnect(page->m_selectAction, SIGNAL(toggled(bool)), this, SLOT(onPageSelectRequest()));
disconnect(page->m_visibleAction, SIGNAL(toggled(bool)), this, SLOT(togglePageVisibleFromAction(bool)));
stack->removeWidget(page->m_widget);
page->setParent(0);
int i = pages.indexOf(page);
if (i >= 0) pages.removeAt(i);
if (guiUpdate) updateTopbar();
}
int TitledPanel::pageCount() const
{
return pages.count();
}
TitledPanelPage *TitledPanel::pageFromId(const QString &id)
{
TitledPanelPage *page = TitledPanelPage::fromId(id);
if (!page || !pages.contains(page))
return 0;
return page;
}
// TODO
void TitledPanel::setHiddenPageIds(const QStringList &hiddenIds)
{
mHiddenPageIds = hiddenIds;
qDebug() << "hidden pages not yet implemented";
}
// TODO
QStringList TitledPanel::hiddenPageIds() const
{
qDebug() << "hidden pages not yet implemented";
return mHiddenPageIds;
}
void TitledPanel::setCurrentPage(const QString &id)
{
TitledPanelPage *page = TitledPanelPage::fromId(id);
if (!page) {
qDebug() << "TitledPanel: trying to access invalid page" << id;
return;
}
if (currentPage() == page)
return;
stack->setCurrentWidget(page->m_widget);
page->m_selectAction->setChecked(true);
updateTopbar();
emit pageChanged(id);
}
TitledPanelPage *TitledPanel::currentPage() const
{
QWidget *w = stack->currentWidget();
if (!w) return 0;
return qvariant_cast<TitledPanelPage *>(w->property("containingPage"));
}
QString TitledPanel::currentPageId() const
{
TitledPanelPage *page = currentPage();
if (page) return page->id();
return QString();
}
void TitledPanel::showPage(const QString &id)
{
setCurrentPage(id);
show();
}
QAction *TitledPanel::toggleViewAction() const
{
return mToggleViewAction;
}
void TitledPanel::setSelectorStyle(TitledPanel::PageSelectorStyle style)
{
selectorStyle = style;
updateTopbar();
}
void TitledPanel::setVisible(bool visible)
{
QFrame::setVisible(visible);
mToggleViewAction->setChecked(visible);
}
void TitledPanel::updateTopbar()
{
QToolBar *oldTopbar = topbar;
// did not manage to remove and add again widgets to the toolbar properly
// widgets added by toolbar->addWidget seem to have a bit weird behaviour
// on toolbar->clear() or reparenting
// so we create a new toolbar
// alternatively do not use a toolbar, but a widget styled like one, as QtCreator does
topbar = new QToolBar(this);
topbar->setOrientation(Qt::Horizontal);
topbar->setFloatable(false);
topbar->setMovable(false);
topbar->setIconSize(QSize(16, 16));
topbar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
QList<QAction *> acts = pageSelectActions->actions();
foreach (QAction *act, acts) {
pageSelectActions->removeAction(act);
}
int visiblePageCount = 0;
TitledPanelPage *firstVisiblePage = 0;
foreach (TitledPanelPage *p, pages) {
if (p->visible()) {
if (!firstVisiblePage) firstVisiblePage = p;
visiblePageCount++;
}
}
// will be deleted together with oldTopbar because they are children of it
lbTopbarLabel = 0;
cbTopbarSelector = 0;
tbTopbarSelector = 0;
if (visiblePageCount == 1) {
lbTopbarLabel = new QLabel(firstVisiblePage->m_title);
topbar->addWidget(lbTopbarLabel);
} else if (visiblePageCount > 1) {
switch (selectorStyle) {
case ComboSelector:
cbTopbarSelector = new QComboBox(topbar);
cbTopbarSelector->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
cbTopbarSelector->setMinimumWidth(0);
cbTopbarSelector->setMaxVisibleItems(25);
connect(cbTopbarSelector, SIGNAL(currentIndexChanged(int)), this, SLOT(setActivePageFromComboBox(int)));
break;
case TabSelector:
tbTopbarSelector = new QTabBar(topbar);
connect(tbTopbarSelector, SIGNAL(currentChanged(int)), this, SLOT(setActivePageFromTabBar(int)));
}
foreach (TitledPanelPage *p, pages) {
if (p->visible()) {
pageSelectActions->addAction(p->m_selectAction);
// create selector (items)
if (cbTopbarSelector) {
bool old = cbTopbarSelector->blockSignals(true);
cbTopbarSelector->addItem(p->m_title, p->id());
cbTopbarSelector->blockSignals(old);
} else if (tbTopbarSelector) {
bool old = tbTopbarSelector->blockSignals(true);
int idx = tbTopbarSelector->addTab(p->m_title);
tbTopbarSelector->setTabData(idx, p->id());
tbTopbarSelector->blockSignals(old);
}
}
}
// update selector
if (cbTopbarSelector) {
int index = cbTopbarSelector->findData(currentPageId());
if (index >= 0) {
cbTopbarSelector->setCurrentIndex(index);
} else {
QString id = cbTopbarSelector->itemData(0).toString();
stack->setCurrentWidget(TitledPanelPage::fromId(id)->m_widget);
}
topbar->addWidget(cbTopbarSelector);
} else if (tbTopbarSelector) {
int index;
for (index = 0; index < tbTopbarSelector->count(); index++) {
if (tbTopbarSelector->tabData(index).toString() == currentPageId()) {
tbTopbarSelector->setCurrentIndex(index);
break;
}
}
if (index >= tbTopbarSelector->count()) { // currentPage not visible any more: fall back to first page
QString id = tbTopbarSelector->tabData(0).toString();
stack->setCurrentWidget(TitledPanelPage::fromId(id)->m_widget);
}
topbar->addWidget(tbTopbarSelector);
#ifdef Q_OS_MAC
topbar->layout()->itemAt(topbar->layout()->count() - 1)->setAlignment(Qt::AlignVCenter);
#else
topbar->layout()->itemAt(topbar->layout()->count() - 1)->setAlignment(Qt::AlignBottom);
#endif
}
/* TODO alternative selectors:
QActionGroup *sel = new QActionGroup(this);
QAction *act = sel->addAction("Hallo");
act->setCheckable(true);
topbar->addAction(act);
act = sel->addAction("Welt");
act->setCheckable(true);
topbar->addAction(act);
act = sel->addAction("Fuzzy");
act->setCheckable(true);
topbar->addAction(act); */
}
if (currentPage()) {
foreach (QAction *act, *currentPage()->m_toolbarActions) {
topbar->addAction(act);
}
}
QWidget *topbarSpacer = new QWidget(topbar);
topbarSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
topbar->addWidget(topbarSpacer);
topbar->addAction(closeAction);
if (oldTopbar) {
oldTopbar->hide();
vLayout->removeWidget(oldTopbar);
oldTopbar->deleteLater();
}
vLayout->insertWidget(0, topbar);
}
void TitledPanel::updatePageSelector(TitledPanelPage *page)
{
if (!page) {
foreach (TitledPanelPage *p, pages) {
updatePageSelector(p);
}
return;
}
if (lbTopbarLabel) {
lbTopbarLabel->setText(page->m_title);
}
if (cbTopbarSelector) {
int index = cbTopbarSelector->findData(page->id());
if (index >= 0) {
cbTopbarSelector->setItemText(index, page->m_title);
}
}
}
void TitledPanel::onPageTitleChange()
{
TitledPanelPage *page = qobject_cast<TitledPanelPage *>(sender());
if (!page) return;
updatePageSelector(page);
}
void TitledPanel::onPageIconChange()
{
QAction *act = qobject_cast<QAction *>(sender());
if (act) {
// TODO update page select controls
qDebug() << "Page icon change not yet implemented";
}
}
void TitledPanel::setActivePageFromAction()
{
QAction *act = qobject_cast<QAction *>(sender());
if (!act) return;
setCurrentPage(act->data().toString());
}
void TitledPanel::setActivePageFromComboBox(int index)
{
QComboBox *box = qobject_cast<QComboBox *>(sender());
if (box)
setCurrentPage(box->itemData(index).toString());
}
void TitledPanel::setActivePageFromTabBar(int index)
{
QTabBar *tabBar = qobject_cast<QTabBar *>(sender());
if (tabBar)
setCurrentPage(tabBar->tabData(index).toString());
}
void TitledPanel::togglePageVisibleFromAction(bool on)
{
Q_UNUSED(on);
QAction *act = qobject_cast<QAction *>(sender());
if (!act || act->data().toString() == "") return;
// TODO maybe just remove(id)
//updateTopbar();
}
// TODO check: can't we directly set the context menu on the widget?
void TitledPanel::customContextMenuRequested(const QPoint &localPosition)
{
QWidget *widget = stack->currentWidget();
if (widget && widget->underMouse()) { //todo?: use a more reliable function than underMouse (see qt bug 260000)
emit widgetContextMenuRequested(widget, mapToGlobal(localPosition));
} else {
QMenu menu;
foreach (TitledPanelPage *page, pages) {
menu.addAction(page->m_visibleAction);
}
menu.exec(mapToGlobal(localPosition));
}
}
|