From 08ec730013715d3b6c6bdb2d61dc0cb454cb2b33 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Sat, 1 Mar 2008 20:43:07 +0000 Subject: [PATCH] Reduce code duplication. Change Profile::property(property).value() to Profile::property(), introduce helper ShouldApplyProperty class to simplify SessionManager::applyProfile() svn path=/trunk/KDE/kdebase/apps/konsole/; revision=781009 --- src/EditProfileDialog.cpp | 36 ++++++++++++++-------------- src/Profile.cpp | 14 +---------- src/Profile.h | 44 ++++++++++++++++++++++++++--------- src/SessionManager.cpp | 49 +++++++++++++++++++-------------------- src/SessionManager.h | 15 ++++++++++++ src/TerminalDisplay.cpp | 12 ++++++---- src/ViewManager.cpp | 31 ++++++++++++++++--------- 7 files changed, 119 insertions(+), 82 deletions(-) diff --git a/src/EditProfileDialog.cpp b/src/EditProfileDialog.cpp index c3aa7c623..1e2d93ba5 100644 --- a/src/EditProfileDialog.cpp +++ b/src/EditProfileDialog.cpp @@ -217,7 +217,7 @@ void EditProfileDialog::setupGeneralPage(const Profile* info) _ui->iconSelectButton->setIcon( KIcon(info->icon()) ); // window options - _ui->showMenuBarButton->setChecked( info->property(Profile::ShowMenuBar).value() ); + _ui->showMenuBarButton->setChecked( info->property(Profile::ShowMenuBar) ); // signals and slots connect( _ui->dirSelectButton , SIGNAL(clicked()) , this , SLOT(selectInitialDir()) ); @@ -243,7 +243,7 @@ void EditProfileDialog::showEnvironmentEditor() KDialog* dialog = new KDialog(this); QTextEdit* edit = new QTextEdit(dialog); - QStringList currentEnvironment = info->property(Profile::Environment).value(); + QStringList currentEnvironment = info->property(Profile::Environment); edit->setPlainText( currentEnvironment.join("\n") ); dialog->setPlainCaption(i18n("Edit Environment")); @@ -262,13 +262,13 @@ void EditProfileDialog::setupTabsPage(const Profile* info) // tab title format _ui->tabTitleEdit->setClearButtonShown(true); _ui->remoteTabTitleEdit->setClearButtonShown(true); - _ui->tabTitleEdit->setText( info->property(Profile::LocalTabTitleFormat).value() ); + _ui->tabTitleEdit->setText( info->property(Profile::LocalTabTitleFormat) ); _ui->remoteTabTitleEdit->setText( - info->property(Profile::RemoteTabTitleFormat).value()); + info->property(Profile::RemoteTabTitleFormat)); // tab options - int tabMode = info->property(Profile::TabBarMode).value(); - int tabPosition = info->property(Profile::TabBarPosition).value(); + int tabMode = info->property(Profile::TabBarMode); + int tabPosition = info->property(Profile::TabBarPosition); // note: Items should be in the same order as the // Profile::TabBarModeEnum enum @@ -480,7 +480,7 @@ void EditProfileDialog::updateKeyBindingsList(bool selectCurrentTranslator) KeyboardTranslatorManager* keyManager = KeyboardTranslatorManager::instance(); const QString& name = lookupProfile() - ->property(Profile::KeyBindings).value(); + ->property(Profile::KeyBindings); const KeyboardTranslator* currentTranslator = keyManager->findTranslator(name); @@ -595,7 +595,7 @@ void EditProfileDialog::preview(int property , const QVariant& value) const Profile* original = lookupProfile(); if (!_previewedProperties.contains(property)) - _previewedProperties.insert(property , original->property((Profile::Property)property) ); + _previewedProperties.insert(property , original->property((Profile::Property)property) ); // temporary change to color scheme SessionManager::instance()->changeProfile( _profileKey , map , false); @@ -796,7 +796,7 @@ void EditProfileDialog::showKeyBindingEditor(bool isNewTranslator) updateKeyBindingsList(); const QString& currentTranslator = lookupProfile() - ->property(Profile::KeyBindings).value(); + ->property(Profile::KeyBindings); if ( newTranslator->name() == currentTranslator ) { @@ -816,7 +816,7 @@ void EditProfileDialog::setupCombo( ComboOption* options , const Profile* profil { while ( options->button != 0 ) { - options->button->setChecked( profile->property((Profile::Property)options->property).value() ); + options->button->setChecked(profile->property((Profile::Property)options->property)); connect( options->button , SIGNAL(toggled(bool)) , this , options->slot ); ++options; @@ -840,7 +840,7 @@ void EditProfileDialog::setupRadio( RadioOption* possible , int actual ) void EditProfileDialog::setupScrollingPage(const Profile* profile) { // setup scrollbar radio - int scrollBarPosition = profile->property(Profile::ScrollBarPosition).value(); + int scrollBarPosition = profile->property(Profile::ScrollBarPosition); RadioOption positions[] = { {_ui->scrollBarHiddenButton,Profile::ScrollBarHidden,SLOT(hideScrollBar())}, {_ui->scrollBarLeftButton,Profile::ScrollBarLeft,SLOT(showScrollBarLeft())}, @@ -851,7 +851,7 @@ void EditProfileDialog::setupScrollingPage(const Profile* profile) setupRadio( positions , scrollBarPosition ); // setup scrollback type radio - int scrollBackType = profile->property(Profile::HistoryMode).value(); + int scrollBackType = profile->property(Profile::HistoryMode); RadioOption types[] = { {_ui->disableScrollbackButton,Profile::DisableHistory,SLOT(noScrollBack())}, {_ui->fixedScrollbackButton,Profile::FixedSizeHistory,SLOT(fixedScrollBack())}, @@ -860,7 +860,7 @@ void EditProfileDialog::setupScrollingPage(const Profile* profile) setupRadio( types , scrollBackType ); // setup scrollback line count spinner - _ui->scrollBackLinesSpinner->setValue( profile->property(Profile::HistorySize).value() ); + _ui->scrollBackLinesSpinner->setValue( profile->property(Profile::HistorySize) ); // signals and slots connect( _ui->scrollBackLinesSpinner , SIGNAL(valueChanged(int)) , this , @@ -910,25 +910,25 @@ void EditProfileDialog::setupAdvancedPage(const Profile* profile) setupCombo( options , profile ); // interaction options - _ui->wordCharacterEdit->setText( profile->property(Profile::WordCharacters).value() ); + _ui->wordCharacterEdit->setText( profile->property(Profile::WordCharacters) ); connect( _ui->wordCharacterEdit , SIGNAL(textChanged(const QString&)) , this , SLOT(wordCharactersChanged(const QString&)) ); // cursor options - if ( profile->property(Profile::UseCustomCursorColor).value() ) + if ( profile->property(Profile::UseCustomCursorColor) ) _ui->customCursorColorButton->setChecked(true); else _ui->autoCursorColorButton->setChecked(true); - _ui->customColorSelectButton->setColor( profile->property(Profile::CustomCursorColor).value() ); + _ui->customColorSelectButton->setColor( profile->property(Profile::CustomCursorColor) ); connect( _ui->customCursorColorButton , SIGNAL(clicked()) , this , SLOT(customCursorColor()) ); connect( _ui->autoCursorColorButton , SIGNAL(clicked()) , this , SLOT(autoCursorColor()) ); connect( _ui->customColorSelectButton , SIGNAL(changed(const QColor&)) , SLOT(customCursorColorChanged(const QColor&)) ); - int shape = profile->property(Profile::CursorShape).value(); + int shape = profile->property(Profile::CursorShape); _ui->cursorShapeCombo->setCurrentIndex(shape); connect( _ui->cursorShapeCombo , SIGNAL(activated(int)) , this , SLOT(setCursorShape(int)) ); @@ -938,7 +938,7 @@ void EditProfileDialog::setupAdvancedPage(const Profile* profile) _ui->selectEncodingButton->setMenu( codecAction->menu() ); connect( codecAction , SIGNAL(triggered(QTextCodec*)) , this , SLOT(setDefaultCodec(QTextCodec*)) ); - _ui->characterEncodingLabel->setText( profile->property(Profile::DefaultEncoding).value() ); + _ui->characterEncodingLabel->setText( profile->property(Profile::DefaultEncoding) ); } void EditProfileDialog::setDefaultCodec(QTextCodec* codec) diff --git a/src/Profile.cpp b/src/Profile.cpp index f05548cc6..14d71dd82 100644 --- a/src/Profile.cpp +++ b/src/Profile.cpp @@ -175,18 +175,6 @@ QHash Profile::setProperties() const { return _propertyValues; } - -QVariant Profile::property(Property property) const -{ - bool canInheritProperty = property != Path; - - if ( _propertyValues.contains(property) ) - return _propertyValues[property]; - else if ( _parent && canInheritProperty ) - return _parent->property(property); - else - return QVariant(); -} void Profile::setProperty(Property property , const QVariant& value) { _propertyValues.insert(property,value); @@ -260,7 +248,7 @@ void KDE4ProfileWriter::writeStandardElement(KConfigGroup& group , const Profil QString name = Profile::primaryNameForProperty(attribute); if ( profile->isPropertySet(attribute) ) - group.writeEntry(name,profile->property(attribute)); + group.writeEntry(name,profile->property(attribute)); } bool KDE4ProfileWriter::writeProfile(const QString& path , const Profile* profile) { diff --git a/src/Profile.h b/src/Profile.h index 6e7f79900..56d074696 100644 --- a/src/Profile.h +++ b/src/Profile.h @@ -264,13 +264,17 @@ public: const Profile* parent() const; /** - * Returns the current value of the specified @p property. + * Returns the current value of the specified @p property, cast to type T. + * Internally properties are stored using the QVariant type and cast to T + * using QVariant::value(); * * If the specified @p property has not been set in this profile, * and a non-null parent was specified in the Profile's constructor, * the parent's value for @p property will be returned. */ - virtual QVariant property(Property property) const; + template + T property(Property property) const; + /** Sets the value of the specified @p property to @p value. */ virtual void setProperty(Property property,const QVariant& value); /** Returns true if the specified property has been set in this Profile instance. */ @@ -300,32 +304,32 @@ public: // /** Convenience method for property(Profile::Path) */ - QString path() const { return property(Profile::Path).value(); } + QString path() const { return property(Profile::Path); } /** Convenience method for property(Profile::Name) */ - QString name() const { return property(Profile::Name).value(); } + QString name() const { return property(Profile::Name); } /** Convenience method for property(Profile::Directory) */ QString defaultWorkingDirectory() const - { return property(Profile::Directory).value(); } + { return property(Profile::Directory); } /** Convenience method for property(Profile::Icon) */ - QString icon() const { return property(Profile::Icon).value(); } + QString icon() const { return property(Profile::Icon); } /** Convenience method for property(Profile::Command) */ - QString command() const { return property(Profile::Command).value(); } + QString command() const { return property(Profile::Command); } /** Convenience method for property(Profile::Arguments) */ - QStringList arguments() const { return property(Profile::Arguments).value(); } + QStringList arguments() const { return property(Profile::Arguments); } /** Convenience method for property(Profile::Font) */ - QFont font() const { return property(Profile::Font).value(); } + QFont font() const { return property(Profile::Font); } /** Convenience method for property(Profile::ColorScheme) */ - QString colorScheme() const { return property(Profile::ColorScheme).value(); } + QString colorScheme() const { return property(Profile::ColorScheme); } /** Convenience method for property(Profile::Environment) */ - QStringList environment() const { return property(Profile::Environment).value(); } + QStringList environment() const { return property(Profile::Environment); } /** * Convenience method. @@ -387,6 +391,24 @@ private: static const PropertyNamePair DefaultPropertyNames[]; }; +template +inline T Profile::property(Property theProperty) const +{ + return property(theProperty).value(); +} +template <> +inline QVariant Profile::property(Property property) const +{ + bool canInheritProperty = property != Path; + + if ( _propertyValues.contains(property) ) + return _propertyValues[property]; + else if ( _parent && canInheritProperty ) + return _parent->property(property); + else + return QVariant(); +} + /** * A profile which contains a number of default settings for various properties. * This can be used as a parent for other profiles or a fallback in case diff --git a/src/SessionManager.cpp b/src/SessionManager.cpp index 25e740d5a..f6f922436 100644 --- a/src/SessionManager.cpp +++ b/src/SessionManager.cpp @@ -380,43 +380,43 @@ void SessionManager::applyProfile(Session* session, const Profile* info , bool m if ( session->profileKey() != key ) session->setProfileKey(key); - // Basic session settings - if ( !modifiedPropertiesOnly || info->isPropertySet(Profile::Name) ) - session->setTitle(Session::NameRole,info->name()); + ShouldApplyProperty apply(info,modifiedPropertiesOnly); - if ( !modifiedPropertiesOnly || info->isPropertySet(Profile::Command) ) + // Basic session settings + if ( apply.shouldApply(Profile::Name) ) + session->setTitle(Session::NameRole,info->name()); + + if ( apply.shouldApply(Profile::Command) ) session->setProgram(info->command()); - if ( !modifiedPropertiesOnly || info->isPropertySet(Profile::Arguments) ) + if ( apply.shouldApply(Profile::Arguments) ) session->setArguments(info->arguments()); - if ( !modifiedPropertiesOnly || info->isPropertySet(Profile::Directory) ) + if ( apply.shouldApply(Profile::Directory) ) session->setInitialWorkingDirectory(info->defaultWorkingDirectory()); - if ( !modifiedPropertiesOnly || info->isPropertySet(Profile::Environment) ) - session->setEnvironment(info->property(Profile::Environment).value()); + if ( apply.shouldApply(Profile::Environment) ) + session->setEnvironment(info->property(Profile::Environment)); - if ( !modifiedPropertiesOnly || info->isPropertySet(Profile::Icon) ) + if ( apply.shouldApply(Profile::Icon) ) session->setIconName(info->icon()); // Key bindings - if ( !modifiedPropertiesOnly || info->isPropertySet(Profile::KeyBindings) ) - session->setKeyBindings(info->property(Profile::KeyBindings).value()); + if ( apply.shouldApply(Profile::KeyBindings) ) + session->setKeyBindings(info->property(Profile::KeyBindings)); // Tab formats - if ( !modifiedPropertiesOnly || info->isPropertySet(Profile::LocalTabTitleFormat) ) + if ( apply.shouldApply(Profile::LocalTabTitleFormat) ) session->setTabTitleFormat( Session::LocalTabTitle , - info->property(Profile::LocalTabTitleFormat).value()); - if ( !modifiedPropertiesOnly || info->isPropertySet(Profile::RemoteTabTitleFormat) ) + info->property(Profile::LocalTabTitleFormat)); + if ( apply.shouldApply(Profile::RemoteTabTitleFormat) ) session->setTabTitleFormat( Session::RemoteTabTitle , - info->property(Profile::RemoteTabTitleFormat).value()); + info->property(Profile::RemoteTabTitleFormat)); // Scrollback / history - if ( !modifiedPropertiesOnly - || info->isPropertySet(Profile::HistoryMode) - || info->isPropertySet(Profile::HistorySize) ) + if ( apply.shouldApply(Profile::HistoryMode) || apply.shouldApply(Profile::HistorySize) ) { - int mode = info->property(Profile::HistoryMode).value(); + int mode = info->property(Profile::HistoryMode); switch ((Profile::HistoryModeEnum)mode) { case Profile::DisableHistory: @@ -424,7 +424,7 @@ void SessionManager::applyProfile(Session* session, const Profile* info , bool m break; case Profile::FixedSizeHistory: { - int lines = info->property(Profile::HistorySize).value(); + int lines = info->property(Profile::HistorySize); session->setHistoryType( HistoryTypeBuffer(lines) ); } break; @@ -435,14 +435,13 @@ void SessionManager::applyProfile(Session* session, const Profile* info , bool m } // Terminal features - if ( !modifiedPropertiesOnly || info->isPropertySet(Profile::FlowControlEnabled) ) - session->setFlowControlEnabled( info->property(Profile::FlowControlEnabled) - .value() ); + if ( apply.shouldApply(Profile::FlowControlEnabled) ) + session->setFlowControlEnabled( info->property(Profile::FlowControlEnabled) ); // Encoding - if ( !modifiedPropertiesOnly || info->isPropertySet(Profile::DefaultEncoding) ) + if ( apply.shouldApply(Profile::DefaultEncoding) ) { - QByteArray name = info->property(Profile::DefaultEncoding).value().toUtf8(); + QByteArray name = info->property(Profile::DefaultEncoding).toUtf8(); session->setCodec( QTextCodec::codecForName(name) ); } } diff --git a/src/SessionManager.h b/src/SessionManager.h index 7efe6dba8..89f4f08bf 100644 --- a/src/SessionManager.h +++ b/src/SessionManager.h @@ -327,5 +327,20 @@ private: QSignalMapper* _sessionMapper; }; +class ShouldApplyProperty +{ +public: + ShouldApplyProperty(const Profile* profile , bool modifiedOnly) : + _profile(profile) , _modifiedPropertiesOnly(modifiedOnly) {} + + bool shouldApply(Profile::Property property) const + { + return !_modifiedPropertiesOnly || _profile->isPropertySet(property); + } +private: + const Profile* _profile; + bool _modifiedPropertiesOnly; +}; + } #endif //SESSIONMANAGER_H diff --git a/src/TerminalDisplay.cpp b/src/TerminalDisplay.cpp index 02462ba18..86b2302d1 100644 --- a/src/TerminalDisplay.cpp +++ b/src/TerminalDisplay.cpp @@ -802,6 +802,8 @@ void TerminalDisplay::scrollImage(int lines , const QRect& screenWindowRegion) linesToMove * _fontHeight )); } + Q_ASSERT(scrollRect.isValid() && !scrollRect.isEmpty()); + //scroll the display vertically to match internal _image scroll( 0 , _fontHeight * (-lines) , scrollRect ); } @@ -853,8 +855,8 @@ void TerminalDisplay::updateImage() // optimization - scroll the existing image where possible and // avoid expensive text drawing for parts of the image that // can simply be moved up or down - //scrollImage( _screenWindow->scrollCount() , - // _screenWindow->scrollRegion() ); + scrollImage( _screenWindow->scrollCount() , + _screenWindow->scrollRegion() ); _screenWindow->resetScrollCount(); Character* const newimg = _screenWindow->getImage(); @@ -1106,6 +1108,8 @@ void TerminalDisplay::paintEvent( QPaintEvent* pe ) { QPainter paint(this); + qDebug() << "Actually repainting" << pe->region(); + foreach (QRect rect, (pe->region() & contentsRect()).rects()) { drawBackground(paint,rect,palette().background().color(), @@ -1875,7 +1879,7 @@ void TerminalDisplay::extendSelection( const QPoint& position ) i = loc(right.x(),right.y()); if (i>=0 && i<=_imageSize) { selClass = charClass(_image[i-1].character); - if (selClass == ' ') + /* if (selClass == ' ') { while ( right.x() < _usedColumns-1 && charClass(_image[i+1].character) == selClass && (right.y()<_usedLines-1) && !(_lineProperties[right.y()] & LINE_WRAPPED)) @@ -1884,7 +1888,7 @@ void TerminalDisplay::extendSelection( const QPoint& position ) right = left_not_right ? _iPntSelCorr : here; else right.rx()++; // will be balanced later because of offset=-1; - } + }*/ } } diff --git a/src/ViewManager.cpp b/src/ViewManager.cpp index d2ba0c069..0fc1e8654 100644 --- a/src/ViewManager.cpp +++ b/src/ViewManager.cpp @@ -543,7 +543,7 @@ ViewContainer* ViewManager::createContainer(const QString& profileKey) Q_ASSERT( info ); - const int tabPosition = info->property(Profile::TabBarPosition).value(); + const int tabPosition = info->property(Profile::TabBarPosition); ViewContainer::NavigationPosition position = ( tabPosition == Profile::TabBarTop ) ? ViewContainer::NavigationPositionTop : @@ -666,19 +666,28 @@ void ViewManager::applyProfile(TerminalDisplay* view , const QString& profileKey const ColorScheme* colorScheme = colorSchemeForProfile(profileKey); // menu bar visibility - emit setMenuBarVisibleRequest( info->property(Profile::ShowMenuBar).value() ); + emit setMenuBarVisibleRequest( info->property(Profile::ShowMenuBar) ); // tab bar visibility ViewContainer* container = _viewSplitter->activeContainer(); - int tabBarMode = info->property(Profile::TabBarMode).value(); - int tabBarPosition = info->property(Profile::TabBarPosition).value(); + int tabBarMode = info->property(Profile::TabBarMode); + int tabBarPosition = info->property(Profile::TabBarPosition); if ( tabBarMode == Profile::AlwaysHideTabBar ) + { + qDebug() << "Always hide tab bar"; container->setNavigationDisplayMode(ViewContainer::AlwaysHideNavigation); + } else if ( tabBarMode == Profile::AlwaysShowTabBar ) + { + qDebug() << "Always show tab bar"; container->setNavigationDisplayMode(ViewContainer::AlwaysShowNavigation); + } else if ( tabBarMode == Profile::ShowTabBarAsNeeded ) + { + qDebug() << "Show as needed"; container->setNavigationDisplayMode(ViewContainer::ShowNavigationAsNeeded); + } ViewContainer::NavigationPosition position = container->navigationPosition(); @@ -698,11 +707,11 @@ void ViewManager::applyProfile(TerminalDisplay* view , const QString& profileKey view->setOpacity(colorScheme->opacity()); // load font - view->setAntialias(info->property(Profile::AntiAliasFonts).value()); + view->setAntialias(info->property(Profile::AntiAliasFonts)); view->setVTFont(info->font()); // set scroll-bar position - int scrollBarPosition = info->property(Profile::ScrollBarPosition).value(); + int scrollBarPosition = info->property(Profile::ScrollBarPosition); if ( scrollBarPosition == Profile::ScrollBarHidden ) view->setScrollBarPosition(TerminalDisplay::NoScrollBar); @@ -712,11 +721,11 @@ void ViewManager::applyProfile(TerminalDisplay* view , const QString& profileKey view->setScrollBarPosition(TerminalDisplay::ScrollBarRight); // terminal features - bool blinkingCursor = info->property(Profile::BlinkingCursorEnabled).value(); + bool blinkingCursor = info->property(Profile::BlinkingCursorEnabled); view->setBlinkingCursor(blinkingCursor); // cursor shape - int cursorShape = info->property(Profile::CursorShape).value(); + int cursorShape = info->property(Profile::CursorShape); if ( cursorShape == Profile::BlockCursor ) view->setKeyboardCursorShape(TerminalDisplay::BlockCursor); @@ -726,13 +735,13 @@ void ViewManager::applyProfile(TerminalDisplay* view , const QString& profileKey view->setKeyboardCursorShape(TerminalDisplay::UnderlineCursor); // cursor color - bool useCustomColor = info->property(Profile::UseCustomCursorColor).value(); - const QColor& cursorColor = info->property(Profile::CustomCursorColor).value(); + bool useCustomColor = info->property(Profile::UseCustomCursorColor); + const QColor& cursorColor = info->property(Profile::CustomCursorColor); view->setKeyboardCursorColor(!useCustomColor,cursorColor); // word characters - view->setWordCharacters( info->property(Profile::WordCharacters).value() ); + view->setWordCharacters( info->property(Profile::WordCharacters) ); } void ViewManager::updateViewsForSession(Session* session)