ctkSettings.cpp

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Library:   CTK
00004  
00005   Copyright (c) 2010  Kitware Inc.
00006 
00007   Licensed under the Apache License, Version 2.0 (the "License");
00008   you may not use this file except in compliance with the License.
00009   You may obtain a copy of the License at
00010 
00011       http://www.commontk.org/LICENSE
00012 
00013   Unless required by applicable law or agreed to in writing, software
00014   distributed under the License is distributed on an "AS IS" BASIS,
00015   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016   See the License for the specific language governing permissions and
00017   limitations under the License.
00018  
00019 =========================================================================*/
00020 /*=========================================================================
00021 
00022    Program: ParaView
00023    Module:    $RCSfile: pqSettings.cxx,v $
00024 
00025    Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
00026    All rights reserved.
00027 
00028    ParaView is a free software; you can redistribute it and/or modify it
00029    under the terms of the ParaView license version 1.2. 
00030 
00031    See License_v1.2.txt for the full ParaView license.
00032    A copy of this license can be obtained by contacting
00033    Kitware Inc.
00034    28 Corporate Drive
00035    Clifton Park, NY 12065
00036    USA
00037 
00038 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00039 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00040 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00041 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
00042 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00043 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00044 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00045 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00046 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00047 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00048 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00049 
00050 =========================================================================*/
00051 #include "ctkSettings.h"
00052 
00053 // Qt includes
00054 #include <QDialog>
00055 #include <QMainWindow>
00056 #include <QDesktopWidget>
00057 
00058 //-----------------------------------------------------------------------------
00059 ctkSettings::ctkSettings(
00060     const QString& organization, 
00061     const QString& application,
00062     QObject* p) :
00063   QSettings(QSettings::IniFormat, QSettings::UserScope, organization, application, p)
00064 {
00065 }
00066 
00067 //-----------------------------------------------------------------------------
00068 void ctkSettings::alertSettingsModified()
00069 {
00070   emit this->modified();
00071 }
00072 
00073 //-----------------------------------------------------------------------------
00074 void ctkSettings::saveState(const QMainWindow& window, const QString& key)
00075 {
00076   this->beginGroup(key);
00077   this->setValue("Position", window.pos());
00078   this->setValue("Size", window.size());
00079   this->setValue("Layout", window.saveState());
00080   this->endGroup();
00081 }
00082 
00083 //-----------------------------------------------------------------------------
00084 void ctkSettings::saveState(const QDialog& dialog, const QString& key)
00085 {
00086   this->beginGroup(key);
00087   this->setValue("Position", dialog.pos());
00088   this->setValue("Size", dialog.size());
00089   this->endGroup();
00090 }
00091 
00092 //-----------------------------------------------------------------------------
00093 void ctkSettings::restoreState(const QString& key, QMainWindow& window)
00094 {
00095   this->beginGroup(key);
00096   
00097   if(this->contains("Size"))
00098     {
00099     window.resize(this->value("Size").toSize());
00100     }
00101     
00102   if(this->contains("Position"))
00103     {
00104     QPoint windowTopLeft = this->value("Position").toPoint();
00105     QRect mwRect(windowTopLeft, window.size());
00106     
00107     QDesktopWidget desktop;
00108     QRect desktopRect = desktop.availableGeometry( desktop.primaryScreen() );
00109     // try moving it to keep size
00110     if(!desktopRect.contains(mwRect))
00111       {
00112       mwRect = QRect(desktopRect.topLeft(), window.size());
00113       }
00114     // still doesn't fit, resize it
00115     if(!desktopRect.contains(mwRect))
00116       {
00117       mwRect = QRect(desktopRect.topLeft(), window.size());
00118       window.resize(desktopRect.size());
00119       }
00120     window.move(mwRect.topLeft());
00121     }
00122 
00123   if(this->contains("Layout"))
00124     {
00125     window.restoreState(this->value("Layout").toByteArray());
00126     }
00127   
00128   this->endGroup();
00129 }
00130 
00131 //-----------------------------------------------------------------------------
00132 void ctkSettings::restoreState(const QString& key, QDialog& dialog)
00133 {
00134   this->beginGroup(key);
00135   
00136   if(this->contains("Size"))
00137     {
00138     dialog.resize(this->value("Size").toSize());
00139     }
00140     
00141   if(this->contains("Position"))
00142     {
00143     dialog.move(this->value("Position").toPoint());
00144     }
00145 
00146   this->endGroup();
00147 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines