KTutorial  0.5.1
StepWidget.cpp
00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00003  *   Copyright (C) 2009 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00004  *   Copyright (C) 2010 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00005  *   Copyright (C) 2012 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; If not, see <http://www.gnu.org/licenses/>.  *
00019  ***************************************************************************/
00020 
00021 #include "StepWidget.h"
00022 
00023 #include <QHBoxLayout>
00024 #include <QKeyEvent>
00025 #include <QMouseEvent>
00026 #include <QWhatsThis>
00027 
00028 #include <KLocalizedString>
00029 
00030 #include "ui_StepWidget.h"
00031 #include "WindowOnTopEnforcer.h"
00032 #include "../Option.h"
00033 #include "../Step.h"
00034 
00035 namespace ktutorial {
00036 namespace view {
00037 
00038 //public:
00039 
00040 StepWidget::StepWidget(const QString& tutorialName, QWidget* parent /*= 0*/):
00041         QFrame(parent, Qt::Dialog | Qt::FramelessWindowHint),
00042     mDragging(false) {
00043 
00044     setFrameShape(QFrame::Panel);
00045     setLineWidth(1);
00046 
00047     ui = new Ui::StepWidget();
00048     ui->setupUi(this);
00049 
00050     setWindowTitle(i18nc("@title:window", "Tutorial: %1", tutorialName));
00051 
00052     ui->closeButton->setIcon(KIcon("dialog-close"));
00053 
00054     QSize buttonSize = ui->closeButton->size();
00055     buttonSize.setWidth(buttonSize.height());
00056 //     buttonSize.setWidth(ui.closeButton->iconSize().width());
00057 //     buttonSize.setHeight(ui.closeButton->iconSize().height());
00058 
00059     ui->closeButton->setFixedSize(buttonSize);
00060 
00061     connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(close()));
00062 
00063     mOptionsLayout = new QHBoxLayout(ui->optionsWidget);
00064 }
00065 
00066 StepWidget::~StepWidget() {
00067     delete ui;
00068 }
00069 
00070 void StepWidget::setMainApplicationWindow(QWidget* mainApplicationWindow) {
00071     WindowOnTopEnforcer* windowOnTopEnforcer = new WindowOnTopEnforcer(this);
00072     windowOnTopEnforcer->setBaseWindow(mainApplicationWindow);
00073 }
00074 
00075 //public slots:
00076 
00077 void StepWidget::setStep(Step* step) {
00078     ui->textWidget->setText(step->text());
00079     setOptions(step->options());
00080 
00081     adjustSize();
00082 
00083     //Shown after adjusting the size so it is centered on the widget. If shown
00084     //before, it'll use the ui file size, and it won't be centered
00085     if (isHidden()) {
00086         show();
00087     }
00088 }
00089 
00090 //protected:
00091 
00092 void StepWidget::closeEvent(QCloseEvent* event) {
00093     QWidget::closeEvent(event);
00094     emit finished();
00095     deleteLater();
00096 }
00097 
00098 void StepWidget::keyPressEvent(QKeyEvent* event) {
00099     if (event->modifiers() == Qt::NoModifier &&
00100                 event->key() == Qt::Key_Escape) {
00101         ui->closeButton->animateClick();
00102         event->accept();
00103         return;
00104     } else if (event->modifiers() == Qt::ShiftModifier &&
00105                event->key() == Qt::Key_F1) {
00106         QWhatsThis::enterWhatsThisMode();
00107         event->accept();
00108         return;
00109     }
00110 
00111     QWidget::keyPressEvent(event);
00112 }
00113 
00114 void StepWidget::mouseMoveEvent(QMouseEvent* event) {
00115     if (!mDragging || this != mouseGrabber()) {
00116         return;
00117     }
00118 
00119     move(event->globalPos() - mDragOffset);
00120 }
00121 
00122 void StepWidget::mousePressEvent(QMouseEvent* event) {
00123     if (event->button() != Qt::LeftButton || mDragging) {
00124         return;
00125     }
00126 
00127     mDragOffset = event->pos();
00128 
00129     grabMouse(Qt::SizeAllCursor);
00130     mDragging = true;
00131 }
00132 
00133 void StepWidget::mouseReleaseEvent(QMouseEvent* event) {
00134     if (event->button() != Qt::LeftButton || !mDragging) {
00135         return;
00136     }
00137 
00138     mDragging = false;
00139     releaseMouse();
00140 }
00141 
00142 void StepWidget::paintEvent(QPaintEvent* event) {
00143     adjustSize();
00144     QFrame::paintEvent(event);
00145 }
00146 
00147 //private:
00148 
00149 void StepWidget::setOptions(const QList<Option*>& options) {
00150     QList<KPushButton*> buttons = ui->optionsWidget->findChildren<KPushButton*>();
00151     QListIterator<KPushButton*> itButtons(buttons);
00152     while (itButtons.hasNext()) {
00153         KPushButton* button = itButtons.next();
00154         button->setParent(0);
00155         delete button;
00156     }
00157 
00158     QListIterator<Option*> it(options);
00159     while (it.hasNext()) {
00160         Option* option = it.next();
00161 
00162         KPushButton* button = new KPushButton(ui->optionsWidget);
00163         button->setText(option->name());
00164         mOptionsLayout->addWidget(button);
00165 
00166         connect(button, SIGNAL(clicked()), option, SIGNAL(selected()));
00167     }
00168 }
00169 
00170 }
00171 }