ctkSettings.cpp 4.4 KB

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