Commit 08ec7300 authored by Robert Knight's avatar Robert Knight
Browse files

Reduce code duplication. Change Profile::property(property).value<T>() to...

Reduce code duplication.  Change Profile::property(property).value<T>() to Profile::property<T>(), introduce helper ShouldApplyProperty class to simplify SessionManager::applyProfile()

svn path=/trunk/KDE/kdebase/apps/konsole/; revision=781009
parent b19855ab
......@@ -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<bool>() );
_ui->showMenuBarButton->setChecked( info->property<bool>(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>();
QStringList currentEnvironment = info->property<QStringList>(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<QString>() );
_ui->tabTitleEdit->setText( info->property<QString>(Profile::LocalTabTitleFormat) );
_ui->remoteTabTitleEdit->setText(
info->property(Profile::RemoteTabTitleFormat).value<QString>());
info->property<QString>(Profile::RemoteTabTitleFormat));
// tab options
int tabMode = info->property(Profile::TabBarMode).value<int>();
int tabPosition = info->property(Profile::TabBarPosition).value<int>();
int tabMode = info->property<int>(Profile::TabBarMode);
int tabPosition = info->property<int>(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<QString>();
->property<QString>(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<QVariant>((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<QString>();
->property<QString>(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<bool>() );
options->button->setChecked(profile->property<bool>((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>();
int scrollBarPosition = profile->property<int>(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>();
int scrollBackType = profile->property<int>(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<int>() );
_ui->scrollBackLinesSpinner->setValue( profile->property<int>(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<QString>() );
_ui->wordCharacterEdit->setText( profile->property<QString>(Profile::WordCharacters) );
connect( _ui->wordCharacterEdit , SIGNAL(textChanged(const QString&)) , this ,
SLOT(wordCharactersChanged(const QString&)) );
// cursor options
if ( profile->property(Profile::UseCustomCursorColor).value<bool>() )
if ( profile->property<bool>(Profile::UseCustomCursorColor) )
_ui->customCursorColorButton->setChecked(true);
else
_ui->autoCursorColorButton->setChecked(true);
_ui->customColorSelectButton->setColor( profile->property(Profile::CustomCursorColor).value<QColor>() );
_ui->customColorSelectButton->setColor( profile->property<QColor>(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>();
int shape = profile->property<int>(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<QString>() );
_ui->characterEncodingLabel->setText( profile->property<QString>(Profile::DefaultEncoding) );
}
void EditProfileDialog::setDefaultCodec(QTextCodec* codec)
......
......@@ -175,18 +175,6 @@ QHash<Profile::Property,QVariant> 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<QVariant>(attribute));
}
bool KDE4ProfileWriter::writeProfile(const QString& path , const Profile* profile)
{
......
......@@ -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<T>();
*
* 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 <class T>
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>(); }
QString path() const { return property<QString>(Profile::Path); }
/** Convenience method for property(Profile::Name) */
QString name() const { return property(Profile::Name).value<QString>(); }
QString name() const { return property<QString>(Profile::Name); }
/** Convenience method for property(Profile::Directory) */
QString defaultWorkingDirectory() const
{ return property(Profile::Directory).value<QString>(); }
{ return property<QString>(Profile::Directory); }
/** Convenience method for property(Profile::Icon) */
QString icon() const { return property(Profile::Icon).value<QString>(); }
QString icon() const { return property<QString>(Profile::Icon); }
/** Convenience method for property(Profile::Command) */
QString command() const { return property(Profile::Command).value<QString>(); }
QString command() const { return property<QString>(Profile::Command); }
/** Convenience method for property(Profile::Arguments) */
QStringList arguments() const { return property(Profile::Arguments).value<QStringList>(); }
QStringList arguments() const { return property<QStringList>(Profile::Arguments); }
/** Convenience method for property(Profile::Font) */
QFont font() const { return property(Profile::Font).value<QFont>(); }
QFont font() const { return property<QFont>(Profile::Font); }
/** Convenience method for property(Profile::ColorScheme) */
QString colorScheme() const { return property(Profile::ColorScheme).value<QString>(); }
QString colorScheme() const { return property<QString>(Profile::ColorScheme); }
/** Convenience method for property(Profile::Environment) */
QStringList environment() const { return property(Profile::Environment).value<QStringList>(); }
QStringList environment() const { return property<QStringList>(Profile::Environment); }
/**
* Convenience method.
......@@ -387,6 +391,24 @@ private:
static const PropertyNamePair DefaultPropertyNames[];
};
template <class T>
inline T Profile::property(Property theProperty) const
{
return property<QVariant>(theProperty).value<T>();
}
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<QVariant>(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
......
......@@ -380,43 +380,43 @@ void SessionManager::applyProfile(Session* session, const Profile* info , bool m
if ( session->profileKey() != key )
session->setProfileKey(key);
ShouldApplyProperty apply(info,modifiedPropertiesOnly);
// Basic session settings
if ( !modifiedPropertiesOnly || info->isPropertySet(Profile::Name) )
session->setTitle(Session::NameRole,info->name());
if ( apply.shouldApply(Profile::Name) )
session->setTitle(Session::NameRole,info->name());
if ( !modifiedPropertiesOnly || info->isPropertySet(Profile::Command) )
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<QStringList>());
if ( apply.shouldApply(Profile::Environment) )
session->setEnvironment(info->property<QStringList>(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<QString>());
if ( apply.shouldApply(Profile::KeyBindings) )
session->setKeyBindings(info->property<QString>(Profile::KeyBindings));
// Tab formats
if ( !modifiedPropertiesOnly || info->isPropertySet(Profile::LocalTabTitleFormat) )
if ( apply.shouldApply(Profile::LocalTabTitleFormat) )
session->setTabTitleFormat( Session::LocalTabTitle ,
info->property(Profile::LocalTabTitleFormat).value<QString>());
if ( !modifiedPropertiesOnly || info->isPropertySet(Profile::RemoteTabTitleFormat) )
info->property<QString>(Profile::LocalTabTitleFormat));
if ( apply.shouldApply(Profile::RemoteTabTitleFormat) )
session->setTabTitleFormat( Session::RemoteTabTitle ,
info->property(Profile::RemoteTabTitleFormat).value<QString>());
info->property<QString>(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>();
int mode = info->property<int>(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>();
int lines = info->property<int>(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<bool>() );
if ( apply.shouldApply(Profile::FlowControlEnabled) )
session->setFlowControlEnabled( info->property<bool>(Profile::FlowControlEnabled) );
// Encoding
if ( !modifiedPropertiesOnly || info->isPropertySet(Profile::DefaultEncoding) )
if ( apply.shouldApply(Profile::DefaultEncoding) )
{
QByteArray name = info->property(Profile::DefaultEncoding).value<QString>().toUtf8();
QByteArray name = info->property<QString>(Profile::DefaultEncoding).toUtf8();
session->setCodec( QTextCodec::codecForName(name) );
}
}
......
......@@ -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
......@@ -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;
}
}*/
}
}
......
......@@ -543,7 +543,7 @@ ViewContainer* ViewManager::createContainer(const QString& profileKey)
Q_ASSERT( info );
const int tabPosition = info->property(Profile::TabBarPosition).value<int>();
const int tabPosition = info->property<int>(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<bool>() );
emit setMenuBarVisibleRequest( info->property<bool>(Profile::ShowMenuBar) );
// tab bar visibility
ViewContainer* container = _viewSplitter->activeContainer();
int tabBarMode = info->property(Profile::TabBarMode).value<int>();
int tabBarPosition = info->property(Profile::TabBarPosition).value<int>();
int tabBarMode = info->property<int>(Profile::TabBarMode);
int tabBarPosition = info->property<int>(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<bool>());
view->setAntialias(info->property<bool>(Profile::AntiAliasFonts));
view->setVTFont(info->font());
// set scroll-bar position
int scrollBarPosition = info->property(Profile::ScrollBarPosition).value<int>();
int scrollBarPosition = info->property<int>(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>();
bool blinkingCursor = info->property<bool>(Profile::BlinkingCursorEnabled);
view->setBlinkingCursor(blinkingCursor);
// cursor shape
int cursorShape = info->property(Profile::CursorShape).value<int>();
int cursorShape = info->property<int>(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<bool>();
const QColor& cursorColor = info->property(Profile::CustomCursorColor).value<QColor>();
bool useCustomColor = info->property<bool>(Profile::UseCustomCursorColor);
const QColor& cursorColor = info->property<QColor>(Profile::CustomCursorColor);
view->setKeyboardCursorColor(!useCustomColor,cursorColor);
// word characters
view->setWordCharacters( info->property(Profile::WordCharacters).value<QString>() );
view->setWordCharacters( info->property<QString>(Profile::WordCharacters) );
}
void ViewManager::updateViewsForSession(Session* session)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment