ctkSettings.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.commontk.org/LICENSE
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. /*=========================================================================
  15. Program: ParaView
  16. Module: $RCSfile: pqSettings.cxx,v $
  17. Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
  18. All rights reserved.
  19. ParaView is a free software; you can redistribute it and/or modify it
  20. under the terms of the ParaView license version 1.2.
  21. See License_v1.2.txt for the full ParaView license.
  22. A copy of this license can be obtained by contacting
  23. Kitware Inc.
  24. 28 Corporate Drive
  25. Clifton Park, NY 12065
  26. USA
  27. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. =========================================================================*/
  39. #include "ctkSettings.h"
  40. // Qt includes
  41. #include <QDialog>
  42. #include <QMainWindow>
  43. #include <QDesktopWidget>
  44. //-----------------------------------------------------------------------------
  45. ctkSettings::ctkSettings(
  46. const QString& organization,
  47. const QString& application,
  48. QObject* p) :
  49. QSettings(QSettings::IniFormat, QSettings::UserScope, organization, application, p)
  50. {
  51. }
  52. //-----------------------------------------------------------------------------
  53. void ctkSettings::alertSettingsModified()
  54. {
  55. emit this->modified();
  56. }
  57. //-----------------------------------------------------------------------------
  58. void ctkSettings::saveState(const QMainWindow& window, const QString& key)
  59. {
  60. this->beginGroup(key);
  61. this->setValue("Position", window.pos());
  62. this->setValue("Size", window.size());
  63. this->setValue("Layout", window.saveState());
  64. this->endGroup();
  65. }
  66. //-----------------------------------------------------------------------------
  67. void ctkSettings::saveState(const QDialog& dialog, const QString& key)
  68. {
  69. this->beginGroup(key);
  70. this->setValue("Position", dialog.pos());
  71. this->setValue("Size", dialog.size());
  72. this->endGroup();
  73. }
  74. //-----------------------------------------------------------------------------
  75. void ctkSettings::restoreState(const QString& key, QMainWindow& window)
  76. {
  77. this->beginGroup(key);
  78. if(this->contains("Size"))
  79. {
  80. window.resize(this->value("Size").toSize());
  81. }
  82. if(this->contains("Position"))
  83. {
  84. QPoint windowTopLeft = this->value("Position").toPoint();
  85. QRect mwRect(windowTopLeft, window.size());
  86. QDesktopWidget desktop;
  87. QRect desktopRect = desktop.availableGeometry( desktop.primaryScreen() );
  88. // try moving it to keep size
  89. if(!desktopRect.contains(mwRect))
  90. {
  91. mwRect = QRect(desktopRect.topLeft(), window.size());
  92. }
  93. // still doesn't fit, resize it
  94. if(!desktopRect.contains(mwRect))
  95. {
  96. mwRect = QRect(desktopRect.topLeft(), window.size());
  97. window.resize(desktopRect.size());
  98. }
  99. window.move(mwRect.topLeft());
  100. }
  101. if(this->contains("Layout"))
  102. {
  103. window.restoreState(this->value("Layout").toByteArray());
  104. }
  105. this->endGroup();
  106. }
  107. //-----------------------------------------------------------------------------
  108. void ctkSettings::restoreState(const QString& key, QDialog& dialog)
  109. {
  110. this->beginGroup(key);
  111. if(this->contains("Size"))
  112. {
  113. dialog.resize(this->value("Size").toSize());
  114. }
  115. if(this->contains("Position"))
  116. {
  117. dialog.move(this->value("Position").toPoint());
  118. }
  119. this->endGroup();
  120. }