Work on refactoring.

This commit is contained in:
Martin Rotter 2013-08-04 14:16:19 +02:00
parent 5ee4c611a1
commit 61e0ff26db
8 changed files with 54 additions and 23 deletions

View File

@ -148,6 +148,7 @@ void FormMain::setupIcons() {
browser->setupIcons(); browser->setupIcons();
} }
// Setup icons on TabWidget too.
m_ui->m_tabWidget->setupIcons(); m_ui->m_tabWidget->setupIcons();
} }

View File

@ -14,6 +14,7 @@ class TabBar : public QTabBar {
Closable = 1002 Closable = 1002
}; };
// Constructors.
explicit TabBar(QWidget *parent = 0); explicit TabBar(QWidget *parent = 0);
virtual ~TabBar(); virtual ~TabBar();
@ -22,6 +23,7 @@ class TabBar : public QTabBar {
TabBar::TabType tabType(int index); TabBar::TabType tabType(int index);
protected: protected:
// Reimplementations.
void mouseDoubleClickEvent(QMouseEvent *event); void mouseDoubleClickEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event); void mousePressEvent(QMouseEvent *event);

View File

@ -11,14 +11,18 @@ class TabContent : public QWidget {
Q_OBJECT Q_OBJECT
public: public:
TabContent(QWidget *parent = 0); // Contructors.
explicit TabContent(QWidget *parent = 0);
virtual ~TabContent(); virtual ~TabContent();
// Gets/sets current index of this TabContent.
// NOTE: This is the index under which this object lies
// in some TabWidget instance.
virtual int index() const; virtual int index() const;
virtual void setIndex(int index); virtual void setIndex(int index);
// Obtains instance contained in this TabContent or nullptr. // Obtains instance contained in this TabContent or nullptr.
// This is used for obtaining the menu from the instance and so on. // This can be used for obtaining the menu from the instance and so on.
virtual WebBrowser *webBrowser() = 0; virtual WebBrowser *webBrowser() = 0;
private: private:

View File

@ -19,8 +19,6 @@ TabWidget::~TabWidget() {
void TabWidget::createConnections() { void TabWidget::createConnections() {
connect(tabBar(), &QTabBar::tabCloseRequested, this, &TabWidget::closeTab); connect(tabBar(), &QTabBar::tabCloseRequested, this, &TabWidget::closeTab);
// Web browser stuff.
connect(tabBar(), &TabBar::emptySpaceDoubleClicked, connect(tabBar(), &TabBar::emptySpaceDoubleClicked,
this, &TabWidget::addEmptyBrowser); this, &TabWidget::addEmptyBrowser);
} }
@ -39,29 +37,29 @@ void TabWidget::initializeTabs() {
setTabToolTip(index_of_browser, tr("Browse your feeds and messages")); setTabToolTip(index_of_browser, tr("Browse your feeds and messages"));
} }
TabContent *TabWidget::contentAt(int index) { TabContent *TabWidget::widget(int index) const {
return static_cast<TabContent*>(widget(index)); return static_cast<TabContent*>(QTabWidget::widget(index));
} }
void TabWidget::setupIcons() { void TabWidget::setupIcons() {
// Iterate through all tabs and update icons // Iterate through all tabs and update icons
// accordingly. // accordingly.
for (int index = 0; index < count(); index++) { for (int index = 0; index < count(); index++) {
// Index 0 usually contains widget which displays feeds & messages.
if (tabBar()->tabType(index) == TabBar::FeedReader) { if (tabBar()->tabType(index) == TabBar::FeedReader) {
setTabIcon(index, ThemeFactory::getInstance()->fromTheme("application-rss+xml")); setTabIcon(index, ThemeFactory::getInstance()->fromTheme("application-rss+xml"));
} }
// Other indexes probably contain WebBrowsers.
else { else {
WebBrowser *active_browser = contentAt(index)->webBrowser(); WebBrowser *active_browser = widget(index)->webBrowser();
if (active_browser != nullptr) { if (active_browser != nullptr && active_browser->icon().isNull()) {
// We found WebBrowser instance of this tab page. // We found WebBrowser instance of this tab page, which
if (active_browser->icon().isNull()) { // has no suitable icon, load a new one from the icon theme.
// WebBrowser has no suitable icon, load new from icon theme.
setTabIcon(index, ThemeFactory::getInstance()->fromTheme("text-html")); setTabIcon(index, ThemeFactory::getInstance()->fromTheme("text-html"));
} }
} }
} }
} }
}
void TabWidget::closeTab(int index) { void TabWidget::closeTab(int index) {
removeTab(index); removeTab(index);
@ -104,10 +102,14 @@ int TabWidget::insertTab(int index, QWidget *widget, const QString &label,
} }
void TabWidget::addEmptyBrowser() { void TabWidget::addEmptyBrowser() {
// TODO: Add reading of move_after_current and make_active
// flags from settings.
addBrowser(false, true); addBrowser(false, true);
} }
void TabWidget::addLinkedBrowser(const QUrl &initial_url) { void TabWidget::addLinkedBrowser(const QUrl &initial_url) {
// TODO: Add reading of move_after_current and make_active
// flags from settings.
addBrowser(true, false, initial_url); addBrowser(true, false, initial_url);
} }
@ -130,7 +132,9 @@ void TabWidget::addBrowser(bool move_after_current,
// Add new browser as the last tab. // Add new browser as the last tab.
final_index = addTab(browser, final_index = addTab(browser,
ThemeFactory::getInstance()->fromTheme("text-html"), ThemeFactory::getInstance()->fromTheme("text-html"),
tr("Web browser"), TabBar::Closable); tr("Web browser"),
TabBar::Closable);
browser->setFocus(Qt::OtherFocusReason);
} }
// Load initial web page if desired. // Load initial web page if desired.

View File

@ -28,9 +28,13 @@ class TabWidget : public QTabWidget {
// Returns tab bar. // Returns tab bar.
TabBar *tabBar(); TabBar *tabBar();
TabContent *contentAt(int index); TabContent *widget(int index) const;
// Initializes TabWidget with tabs, this includes initialization
// of main "Feeds" widget.
void initializeTabs(); void initializeTabs();
// Sets up icons for this TabWidget.
void setupIcons(); void setupIcons();
protected: protected:

View File

@ -80,22 +80,28 @@ void ThemeFactory::loadCurrentIconTheme(bool notify_widgets) {
"icon_theme", "icon_theme",
"mini-kfaenza").toString(); "mini-kfaenza").toString();
// Display list of installed themes.
qDebug("Installed icon themes are: %s.", qDebug("Installed icon themes are: %s.",
qPrintable(installed_themes.join(", "))); qPrintable(installed_themes.join(", ")));
// User wants to load icon theme, but it's not installed.
if (!installed_themes.contains(theme_name_from_settings)) { if (installed_themes.contains(theme_name_from_settings)) {
// Desired icon theme is installed and can be loaded.
qDebug("Loading theme '%s'.", qPrintable(theme_name_from_settings));
QIcon::setThemeName(theme_name_from_settings);
m_currentIconTheme = theme_name_from_settings;
}
else {
// Desired icon theme is not currently available.
// Install "default" icon theme instead.
// NOTE: "Default" icon theme is:
// a) system icon theme on Linux,
// b) no icon theme on other platforms.
qDebug("Icon theme '%s' cannot be loaded because it is not installed. Loading 'default' theme.", qDebug("Icon theme '%s' cannot be loaded because it is not installed. Loading 'default' theme.",
qPrintable(theme_name_from_settings)); qPrintable(theme_name_from_settings));
QIcon::setThemeName(APP_THEME_SYSTEM); QIcon::setThemeName(APP_THEME_SYSTEM);
m_currentIconTheme = APP_THEME_SYSTEM; m_currentIconTheme = APP_THEME_SYSTEM;
} }
// Icon theme is found so it can be installed.
else {
qDebug("Loading theme '%s'.", qPrintable(theme_name_from_settings));
QIcon::setThemeName(theme_name_from_settings);
m_currentIconTheme = theme_name_from_settings;
}
// We need to deliver custom event for all widgets // We need to deliver custom event for all widgets
// to make sure they get a chance to setup their icons. // to make sure they get a chance to setup their icons.

View File

@ -80,6 +80,9 @@ void WebBrowser::createConnections() {
this, &WebBrowser::updateUrl); this, &WebBrowser::updateUrl);
// Connect this WebBrowser to global TabWidget. // Connect this WebBrowser to global TabWidget.
// TODO: Přemýšlet nad tím, zda toto navázání spojení
// nepředělat například do TabWidget::AddBrowser a ty signály
// z m_webView tedy neforwardovat z WebBrowseru.
TabWidget *parent_widget = static_cast<TabWidget*>(parent()); TabWidget *parent_widget = static_cast<TabWidget*>(parent());
connect(m_webView, &BaseWebView::newTabRequested, connect(m_webView, &BaseWebView::newTabRequested,
parent_widget, &TabWidget::addEmptyBrowser); parent_widget, &TabWidget::addEmptyBrowser);
@ -130,6 +133,10 @@ QIcon WebBrowser::icon() {
return m_webView->icon(); return m_webView->icon();
} }
void WebBrowser::setFocus(Qt::FocusReason reason) {
m_txtLocation->setFocus(reason);
}
void WebBrowser::setupIcons() { void WebBrowser::setupIcons() {
m_actionBack->setIcon(ThemeFactory::getInstance()->fromTheme("go-previous")); m_actionBack->setIcon(ThemeFactory::getInstance()->fromTheme("go-previous"));
m_actionForward->setIcon(ThemeFactory::getInstance()->fromTheme("go-next")); m_actionForward->setIcon(ThemeFactory::getInstance()->fromTheme("go-next"));

View File

@ -28,6 +28,9 @@ class WebBrowser : public TabContent {
// Returns icon associated with currently loaded website. // Returns icon associated with currently loaded website.
QIcon icon(); QIcon icon();
// Sets this WebBrowser instance as focused.
void setFocus(Qt::FocusReason reason);
// Returns this instance. // Returns this instance.
// NOTE: This is needed due to TabContent interface. // NOTE: This is needed due to TabContent interface.
WebBrowser *webBrowser(); WebBrowser *webBrowser();