GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
gui-utils.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2000-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <QApplication>
31 #include <QRect>
32 #include <QScreen>
33 
34 #include "gui-utils.h"
35 
37 
38 OCTGUI_API QColor
39 interpolate_color (const QColor& col1, const QColor& col2,
40  double fs, double fv)
41 {
42  qreal h1, s1, v1, h2, s2, v2;
43 
44  col1.getHsvF (&h1, &s1, &v1);
45  col2.getHsvF (&h2, &s2, &v2);
46 
47  return QColor::fromHsvF (h1, s1*fs, v1 + fv*(v2 - v1));
48 }
49 
50 OCTGUI_API void
51 get_screen_geometry (int& width, int& height)
52 {
53  QRect geom = QGuiApplication::primaryScreen ()->availableGeometry ();
54 
55  width = geom.width ();
56  height = geom.height ();
57 }
58 
59 OCTGUI_API void
60 adjust_to_screen (QRect& actual_geometry, const QRect& default_geometry)
61 {
62 
63  // Get the screen that holds the largest part of the given actual geometry
64 
65  const QScreen *actual_screen = nullptr; // no screen found yet
66  QRect actual_screen_geom = QRect (); // geometry of found screen
67  int intersected_area_max = 0; // max. intersected area
68 
69  const int area_actual_geometry
70  = actual_geometry.width () * actual_geometry.height ();
71  QRect intersection;
72 
73  foreach (const QScreen *screen, QGuiApplication::screens())
74  {
75  QRect screen_geom = screen->availableGeometry ();
76  intersection = screen_geom.intersected (actual_geometry);
77  if (! intersection.isEmpty ())
78  {
79  int area = intersection.width () * intersection.height ();
80  if (area > intersected_area_max)
81  {
82  actual_screen = screen;
83  actual_screen_geom = screen->availableGeometry ();
84  if (area == area_actual_geometry)
85  return; // Actual geom is completely on a screen: return
86  intersected_area_max = area;
87  }
88  }
89  }
90 
91  // If the actual geometry is on no screen, use deault geometry
92 
93  if (actual_screen == nullptr)
94  {
95  actual_geometry = default_geometry;
96  return;
97  }
98 
99  // There is a screen that holds a part of the actual geometry.
100  // Now adjust actual geometry to this screen
101 
102  // Get some properties of the actual and intersected geometry
103  int agx1, agy1, agx2, agy2;
104  actual_geometry.getCoords (&agx1, &agy1, &agx2, &agy2);
105  int isx1, isy1, isx2, isy2;
106  intersection.getCoords (&isx1, &isy1, &isx2, &isy2);
107 
108  // Make the intersection the same size as the actual geometry
109  if ((agx1 == isx1) && (agx2 != isx2))
110  isx1 = isx1 - agx2 + isx2;
111  if ((agx1 != isx1) && (agx2 == isx2))
112  isx2 = isx2 + agx2 - isx2;
113  if ((agy1 == isy1) && (agy2 != isy2))
114  isy1 = isy1 - agy2 + isy2;
115  if ((agy1 != isy1) && (agy2 == isy2))
116  isy2 = isy2 + agy2 - isy2;
117 
118  // And compute again the intersection with the screen if this resizing
119  // led to corners outside the screen
120  actual_geometry = actual_screen_geom.intersected (
121  QRect (QPoint (isx1,isy1), QPoint (isx2,isy2)));
122 }
123 
OCTAVE_END_NAMESPACE(octave)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
OCTGUI_API void get_screen_geometry(int &width, int &height)
Definition: gui-utils.cc:51
OCTGUI_API QColor interpolate_color(const QColor &col1, const QColor &col2, double fs, double fv)
Definition: gui-utils.cc:39
OCTGUI_API void adjust_to_screen(QRect &actual_geometry, const QRect &default_geometry)
Definition: gui-utils.cc:60
const octave_char_matrix & v2