12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
- <meta name="author" content="Emanuel Eichhammer" />
- <meta name="copyright" content="(C) 2013-2015 Emanuel Eichhammer" />
- <title>The Layout System</title>
- <link href="qt.css" rel="stylesheet" type="text/css"/>
- </head>
- <body>
- <div id="top">
- <a class="headerLink" href="index.html">Main Page</a> ·
- <a class="headerLink" href="classoverview.html">Class Overview</a> ·
- <a class="headerLink" href="hierarchy.html">Hierarchy</a> ·
- <a class="headerLink" href="annotated.html">All Classes</a> ·
- <a class="headerLink" href="pages.html">Special Pages</a>
- <!-- Generated by Doxygen 1.8.6 -->
- </div><!-- top -->
- <div class="header">
- <div class="headertitle">
- <div class="title">The Layout System </div> </div>
- </div><!--header-->
- <div class="contents">
- <div class="textblock"><p>The layout system is responsible for positioning and scaling layout elements such as axis rects, legends and plot titles in a <a class="el" href="classQCustomPlot.html" title="The central class of the library. This is the QWidget which displays the plot and interacts with the ...">QCustomPlot</a>.</p>
- <h1><a class="anchor" id="layoutsystem-classesandmechanisms"></a>
- Classes and mechanisms</h1>
- <p>The layout system is based on the abstract base class <a class="el" href="classQCPLayoutElement.html">QCPLayoutElement</a>. All objects that take part in the layout system derive from this class, either directly or indirectly.</p>
- <p>Since <a class="el" href="classQCPLayoutElement.html" title="The abstract base class for all objects that form the layout system. ">QCPLayoutElement</a> itself derives from <a class="el" href="classQCPLayerable.html">QCPLayerable</a>, a layout element may draw its own content. However, it is perfectly possible for a layout element to only serve as a structuring and/or positioning element, not drawing anything on its own.</p>
- <h2><a class="anchor" id="layoutsystem-rects"></a>
- Rects of a layout element</h2>
- <p>A layout element is a rectangular object described by two rects: the inner rect (<a class="el" href="classQCPLayoutElement.html#affdfea003469aac3d0fac5f4e06171bc">QCPLayoutElement::rect</a>) and the outer rect (<a class="el" href="classQCPLayoutElement.html#a38975ea13e36de8e53391ce41d94bc0f">QCPLayoutElement::setOuterRect</a>). The inner rect is calculated automatically by applying the margin (<a class="el" href="classQCPLayoutElement.html#a8f450b1f3f992ad576fce2c63d8b79cf">QCPLayoutElement::setMargins</a>) inward from the outer rect. The inner rect is meant for main content while the margin area may either be left blank or serve for displaying peripheral graphics. For example, <a class="el" href="classQCPAxisRect.html">QCPAxisRect</a> positions the four main axes at the sides of the inner rect, so graphs end up inside it and the axis labels and tick labels are in the margin area.</p>
- <h2><a class="anchor" id="layoutsystem-margins"></a>
- Margins</h2>
- <p>Each layout element may provide a mechanism to automatically determine its margins. Internally, this is realized with the <a class="el" href="classQCPLayoutElement.html#a005c9f0fe84bc1591a2cf2c46fd477b4">QCPLayoutElement::calculateAutoMargin</a> function which takes a <a class="el" href="namespaceQCP.html#a7e487e3e2ccb62ab7771065bab7cae54">QCP::MarginSide</a> and returns an integer value which represents the ideal margin for the specified side. The automatic margin will be used on the sides specified in <a class="el" href="classQCPLayoutElement.html#accfda49994e3e6d51ed14504abf9d27d">QCPLayoutElement::setAutoMargins</a>. By default, it is set to <a class="el" href="namespaceQCP.html#a7e487e3e2ccb62ab7771065bab7cae54a43d7361cb0c5244eabdc962021bffebc">QCP::msAll</a> meaning automatic margin calculation is enabled for all four sides. In this case, a minimum margin may be set with <a class="el" href="classQCPLayoutElement.html#a0a8a17abc16b7923159fcc7608f94673">QCPLayoutElement::setMinimumMargins</a>, to prevent the automatic margin mechanism from setting margins smaller than desired for a specific situation. If automatic margin calculation is unset for a specific side, the margin of that side can be controlled directy via <a class="el" href="classQCPLayoutElement.html#a8f450b1f3f992ad576fce2c63d8b79cf">QCPLayoutElement::setMargins</a>.</p>
- <p>If multiple layout ements are arranged next to or beneath each other, it may be desirable to align their inner rects on certain sides. Since they all might have different automatic margins, this usually isn't the case. The class <a class="el" href="classQCPMarginGroup.html">QCPMarginGroup</a> and <a class="el" href="classQCPLayoutElement.html#a516e56f76b6bc100e8e71d329866847d">QCPLayoutElement::setMarginGroup</a> fix this by allowing to synchronize multiple margins. See the documentation there for details.</p>
- <h2><a class="anchor" id="layoutsystem-layout"></a>
- Layouts</h2>
- <p>As mentioned, a <a class="el" href="classQCPLayoutElement.html" title="The abstract base class for all objects that form the layout system. ">QCPLayoutElement</a> may have an arbitrary number of child layout elements and in princple can have the only purpose to manage/arrange those child elements. This is what the subclass <a class="el" href="classQCPLayout.html">QCPLayout</a> specializes on. It is a <a class="el" href="classQCPLayoutElement.html" title="The abstract base class for all objects that form the layout system. ">QCPLayoutElement</a> itself but has no visual representation. It defines an interface to add, remove and manage child layout elements. <a class="el" href="classQCPLayout.html" title="The abstract base class for layouts. ">QCPLayout</a> isn't a usable layout though, it's an abstract base class that concrete layouts derive from, like <a class="el" href="classQCPLayoutGrid.html">QCPLayoutGrid</a> which arranges its child elements in a grid and <a class="el" href="classQCPLayoutInset.html">QCPLayoutInset</a> which allows placing child elements freely inside its rect.</p>
- <p>Since a <a class="el" href="classQCPLayout.html" title="The abstract base class for layouts. ">QCPLayout</a> is a layout element itself, it may be placed inside other layouts. This way, complex hierarchies may be created, offering very flexible arrangements.</p>
- <p>Below is a sketch of the default <a class="el" href="classQCPLayoutGrid.html">QCPLayoutGrid</a> accessible via <a class="el" href="classQCustomPlot.html#afd280d4d621ae64a106543a545c508d7">QCustomPlot::plotLayout</a>. It shows how two child layout elements are placed inside the grid layout next to each other in cells (0, 0) and (0, 1).</p>
- <div class="image">
- <img src="LayoutsystemSketch.png" alt="LayoutsystemSketch.png"/>
- </div>
- <h2><a class="anchor" id="layoutsystem-plotlayout"></a>
- The top level plot layout</h2>
- <p>Every <a class="el" href="classQCustomPlot.html" title="The central class of the library. This is the QWidget which displays the plot and interacts with the ...">QCustomPlot</a> has one top level layout of type <a class="el" href="classQCPLayoutGrid.html">QCPLayoutGrid</a>. It is accessible via <a class="el" href="classQCustomPlot.html#afd280d4d621ae64a106543a545c508d7">QCustomPlot::plotLayout</a> and contains (directly or indirectly via other sub-layouts) all layout elements in the <a class="el" href="classQCustomPlot.html" title="The central class of the library. This is the QWidget which displays the plot and interacts with the ...">QCustomPlot</a>. By default, this top level grid layout contains a single cell which holds the main axis rect.</p>
- <h2><a class="anchor" id="layoutsystem-examples"></a>
- Examples</h2>
- <p><b>Adding a plot title</b> is a typical and simple case to demonstrate basic workings of the layout system. </p>
- <div class="fragment"><div class="line"> <span class="comment">// first we create and prepare a plot title layout element:</span></div>
- <div class="line"> <a class="code" href="classQCPPlotTitle.html">QCPPlotTitle</a> *title = <span class="keyword">new</span> <a class="code" href="classQCPPlotTitle.html">QCPPlotTitle</a>(customPlot);</div>
- <div class="line"> title-><a class="code" href="classQCPPlotTitle.html#aae5a93e88050dfb2cbf6adc087516821">setText</a>(<span class="stringliteral">"Plot Title Example"</span>);</div>
- <div class="line"> title-><a class="code" href="classQCPPlotTitle.html#a199fc7170802ea65006c371875349e37">setFont</a>(QFont(<span class="stringliteral">"sans"</span>, 12, QFont::Bold));</div>
- <div class="line"> <span class="comment">// then we add it to the main plot layout:</span></div>
- <div class="line"> customPlot->plotLayout()->insertRow(0); <span class="comment">// insert an empty row above the axis rect</span></div>
- <div class="line"> customPlot->plotLayout()->addElement(0, 0, title); <span class="comment">// place the title in the empty cell we've just created</span></div>
- </div><!-- fragment --><div class="image">
- <img src="layoutsystem-addingplottitle.png" alt="layoutsystem-addingplottitle.png"/>
- </div>
- <p><b>Arranging multiple axis rects</b> actually is the central purpose of the layout system. </p>
- <div class="fragment"><div class="line"> customPlot->plotLayout()->clear(); <span class="comment">// let's start from scratch and remove the default axis rect</span></div>
- <div class="line"> <span class="comment">// add the first axis rect in second row (row index 1):</span></div>
- <div class="line"> <a class="code" href="classQCPAxisRect.html">QCPAxisRect</a> *bottomAxisRect = <span class="keyword">new</span> <a class="code" href="classQCPAxisRect.html">QCPAxisRect</a>(customPlot);</div>
- <div class="line"> customPlot->plotLayout()->addElement(1, 0, bottomAxisRect);</div>
- <div class="line"> <span class="comment">// create a sub layout that we'll place in first row:</span></div>
- <div class="line"> <a class="code" href="classQCPLayoutGrid.html">QCPLayoutGrid</a> *subLayout = <span class="keyword">new</span> <a class="code" href="classQCPLayoutGrid.html">QCPLayoutGrid</a>;</div>
- <div class="line"> customPlot->plotLayout()-><a class="code" href="classQCPLayoutGrid.html#adff1a2ca691ed83d2d24a4cd1fe17012">addElement</a>(0, 0, subLayout);</div>
- <div class="line"> <span class="comment">// add two axis rects in the sub layout next to each other:</span></div>
- <div class="line"> <a class="code" href="classQCPAxisRect.html">QCPAxisRect</a> *leftAxisRect = <span class="keyword">new</span> <a class="code" href="classQCPAxisRect.html">QCPAxisRect</a>(customPlot);</div>
- <div class="line"> <a class="code" href="classQCPAxisRect.html">QCPAxisRect</a> *rightAxisRect = <span class="keyword">new</span> <a class="code" href="classQCPAxisRect.html">QCPAxisRect</a>(customPlot);</div>
- <div class="line"> subLayout-><a class="code" href="classQCPLayoutGrid.html#adff1a2ca691ed83d2d24a4cd1fe17012">addElement</a>(0, 0, leftAxisRect);</div>
- <div class="line"> subLayout-><a class="code" href="classQCPLayoutGrid.html#adff1a2ca691ed83d2d24a4cd1fe17012">addElement</a>(0, 1, rightAxisRect);</div>
- <div class="line"> subLayout-><a class="code" href="classQCPLayoutGrid.html#ae38f31a71687b9d7ee3104852528fb50">setColumnStretchFactor</a>(0, 3); <span class="comment">// left axis rect shall have 60% of width</span></div>
- <div class="line"> subLayout-><a class="code" href="classQCPLayoutGrid.html#ae38f31a71687b9d7ee3104852528fb50">setColumnStretchFactor</a>(1, 2); <span class="comment">// right one only 40% (3:2 = 60:40)</span></div>
- <div class="line"> <span class="comment">// since we've created the axis rects and axes from scratch, we need to place them on</span></div>
- <div class="line"> <span class="comment">// according layers, if we don't want the grid to be drawn above the axes etc.</span></div>
- <div class="line"> <span class="comment">// place the axis on "axes" layer and grids on the "grid" layer, which is below "axes":</span></div>
- <div class="line"> QList<QCPAxis*> allAxes;</div>
- <div class="line"> allAxes << bottomAxisRect-><a class="code" href="classQCPAxisRect.html#a66654d51ca611ef036ded36250cd2518">axes</a>() << leftAxisRect-><a class="code" href="classQCPAxisRect.html#a66654d51ca611ef036ded36250cd2518">axes</a>() << rightAxisRect-><a class="code" href="classQCPAxisRect.html#a66654d51ca611ef036ded36250cd2518">axes</a>();</div>
- <div class="line"> <span class="keywordflow">foreach</span> (<a class="code" href="classQCPAxis.html">QCPAxis</a> *axis, allAxes)</div>
- <div class="line"> {</div>
- <div class="line"> axis-><a class="code" href="classQCPLayerable.html#ab0d0da6d2de45a118886d2c8e16d5a54">setLayer</a>(<span class="stringliteral">"axes"</span>);</div>
- <div class="line"> axis-><a class="code" href="classQCPAxis.html#ac4fb913cce3072b5e75a4635e0f6cd04">grid</a>()-><a class="code" href="classQCPLayerable.html#ab0d0da6d2de45a118886d2c8e16d5a54">setLayer</a>(<span class="stringliteral">"grid"</span>);</div>
- <div class="line"> }</div>
- </div><!-- fragment --><div class="image">
- <img src="layoutsystem-multipleaxisrects.png" alt="layoutsystem-multipleaxisrects.png"/>
- </div>
- </div></div><!-- contents -->
- </body>
- </html>
|