welcomemode: simplify WelcomeMode

This commit is contained in:
hjk
2011-04-13 17:09:44 +02:00
parent be9455bc50
commit ba52d6725b
7 changed files with 223 additions and 314 deletions

View File

@@ -32,24 +32,226 @@
**************************************************************************/
#include "welcomeplugin.h"
#include "welcomemode.h"
#include "communitywelcomepage.h"
#include "communitywelcomepage.h"
#include "ui_welcomemode.h"
#include <extensionsystem/pluginmanager.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/icore.h>
#include <coreplugin/imode.h>
#include <coreplugin/modemanager.h>
#include <utils/styledbar.h>
#include <utils/welcomemodetreewidget.h>
#include <utils/iwelcomepage.h>
#include <QtGui/QScrollArea>
#include <QtGui/QDesktopServices>
#include <QtGui/QToolButton>
#include <QtGui/QPainter>
#include <QtCore/QSettings>
#include <QtCore/QDebug>
#include <QtCore/QUrl>
#include <QtCore/QtPlugin>
using namespace Welcome::Internal;
enum { debug = 0 };
using namespace ExtensionSystem;
namespace Utils {
class IWelcomePage;
}
namespace Welcome {
namespace Internal {
// Helper class introduced to cache the scaled background image
// so we avoid re-scaling for every repaint.
class ImageWidget : public QWidget
{
public:
ImageWidget(const QImage &bg, QWidget *parent) : QWidget(parent), m_bg(bg) {}
void paintEvent(QPaintEvent *e) {
if (!m_bg.isNull()) {
QPainter painter(this);
if (m_stretch.size() != size())
m_stretch = QPixmap::fromImage(m_bg.scaled(size(),
Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
if (!m_stretch.size().isEmpty())
painter.drawPixmap(rect(), m_stretch);
}
QWidget::paintEvent(e);
}
private:
QImage m_bg;
QPixmap m_stretch;
};
class WelcomeMode : public Core::IMode
{
Q_OBJECT
public:
WelcomeMode();
~WelcomeMode();
void activated();
void initPlugins();
private slots:
void slotFeedback();
void welcomePluginAdded(QObject*);
void showClickedPage();
private:
QToolButton *addPageToolButton(Utils::IWelcomePage *plugin, int position = -1);
typedef QMap<QToolButton *, QWidget *> ToolButtonWidgetMap;
QScrollArea *m_scrollArea;
QWidget *m_outerWidget;
ImageWidget *m_welcomePage;
ToolButtonWidgetMap buttonMap;
QHBoxLayout * buttonLayout;
Ui::WelcomeMode ui;
};
static const char currentPageSettingsKeyC[] = "General/WelcomeTab";
// --- WelcomeMode
WelcomeMode::WelcomeMode()
{
setDisplayName(tr("Welcome"));
setIcon(QIcon(QLatin1String(Core::Constants::ICON_QTLOGO_32)));
setPriority(Core::Constants::P_MODE_WELCOME);
setId(QLatin1String(Core::Constants::MODE_WELCOME));
setType(QLatin1String(Core::Constants::MODE_WELCOME_TYPE));
setContextHelpId(QLatin1String("Qt Creator Manual"));
m_outerWidget = new QWidget;
QVBoxLayout *l = new QVBoxLayout(m_outerWidget);
l->setMargin(0);
l->setSpacing(0);
l->addWidget(new Utils::StyledBar(m_outerWidget));
m_welcomePage = new ImageWidget(QImage(":/welcome/images/welcomebg.png"), m_outerWidget);
ui.setupUi(m_welcomePage);
ui.helpUsLabel->setAttribute(Qt::WA_LayoutUsesWidgetRect);
ui.feedbackButton->setAttribute(Qt::WA_LayoutUsesWidgetRect);
l->addWidget(m_welcomePage);
m_scrollArea = new QScrollArea;
m_scrollArea->setFrameStyle(QFrame::NoFrame);
m_scrollArea->setWidget(m_outerWidget);
m_scrollArea->setWidgetResizable(true);
setContext(Core::Context(Core::Constants::C_WELCOME_MODE));
setWidget(m_scrollArea);
PluginManager *pluginManager = PluginManager::instance();
connect(pluginManager, SIGNAL(objectAdded(QObject*)), SLOT(welcomePluginAdded(QObject*)));
connect(ui.feedbackButton, SIGNAL(clicked()), SLOT(slotFeedback()));
}
WelcomeMode::~WelcomeMode()
{
QSettings *settings = Core::ICore::instance()->settings();
settings->setValue(QLatin1String(currentPageSettingsKeyC), ui.stackedWidget->currentIndex());
delete m_outerWidget;
}
bool sortFunction(Utils::IWelcomePage *a, Utils::IWelcomePage *b)
{
return a->priority() < b->priority();
}
// Create a QToolButton for a page
QToolButton *WelcomeMode::addPageToolButton(Utils::IWelcomePage *plugin, int position)
{
QToolButton *btn = new QToolButton;
btn->setCheckable(true);
btn->setText(plugin->title());
btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
btn->setAutoExclusive(true);
connect (btn, SIGNAL(clicked()), SLOT(showClickedPage()));
buttonMap.insert(btn, plugin->page());
if (position >= 0) {
buttonLayout->insertWidget(position, btn);
} else {
buttonLayout->addWidget(btn);
}
return btn;
}
void WelcomeMode::initPlugins()
{
buttonLayout = new QHBoxLayout(ui.navFrame);
buttonLayout->setMargin(0);
buttonLayout->setSpacing(0);
QList<Utils::IWelcomePage*> plugins = PluginManager::instance()->getObjects<Utils::IWelcomePage>();
qSort(plugins.begin(), plugins.end(), &sortFunction);
foreach (Utils::IWelcomePage *plugin, plugins) {
ui.stackedWidget->addWidget(plugin->page());
addPageToolButton(plugin);
if (debug)
qDebug() << "WelcomeMode::initPlugins" << plugin->title();
}
QSettings *settings = Core::ICore::instance()->settings();
const int tabId = settings->value(QLatin1String(currentPageSettingsKeyC), 0).toInt();
const int pluginCount = ui.stackedWidget->count();
if (tabId >= 0 && tabId < pluginCount) {
ui.stackedWidget->setCurrentIndex(tabId);
if (QToolButton *btn = buttonMap.key(ui.stackedWidget->currentWidget()))
btn->setChecked(true);
}
}
void WelcomeMode::welcomePluginAdded(QObject *obj)
{
if (Utils::IWelcomePage *plugin = qobject_cast<Utils::IWelcomePage*>(obj)) {
int insertPos = 0;
foreach (Utils::IWelcomePage* p, PluginManager::instance()->getObjects<Utils::IWelcomePage>()) {
if (plugin->priority() < p->priority())
insertPos++;
else
break;
}
ui.stackedWidget->insertWidget(insertPos, plugin->page());
addPageToolButton(plugin, insertPos);
if (debug)
qDebug() << "welcomePluginAdded" << plugin->title() << "at" << insertPos
<< " of " << buttonMap.size();
}
}
void WelcomeMode::showClickedPage()
{
QToolButton *btn = qobject_cast<QToolButton*>(sender());
const ToolButtonWidgetMap::const_iterator it = buttonMap.constFind(btn);
if (it != buttonMap.constEnd())
ui.stackedWidget->setCurrentWidget(it.value());
}
void WelcomeMode::slotFeedback()
{
QDesktopServices::openUrl(QUrl(QLatin1String(
"http://qt.nokia.com/forms/feedback-forms/qt-creator-user-feedback/view")));
}
//
WelcomePlugin::WelcomePlugin()
: m_welcomeMode(0)
{
}
WelcomePlugin::~WelcomePlugin()
{
}
/*! Initializes the plugin. Returns true on success.
Plugins want to register objects with the plugin manager here.
@@ -83,4 +285,10 @@ void WelcomePlugin::extensionsInitialized()
Core::ModeManager::instance()->activateMode(m_welcomeMode->id());
}
Q_EXPORT_PLUGIN(WelcomePlugin)
} // namespace Welcome
} // namespace Internal
Q_EXPORT_PLUGIN(Welcome::Internal::WelcomePlugin)
#include "welcomeplugin.moc"