YSTest: viewer.hpp 源文件

YSTest PreAlpha_b500_20140530 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
viewer.hpp
浏览该文件的文档.
1 /*
2 © 2011-2014 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef YSL_INC_UI_viewer_hpp_
29 #define YSL_INC_UI_viewer_hpp_ 1
30
31 #include "YModules.h"
32 #include YFM_YSLib_UI_YComponent
33 #include <ystdex/algorithm.hpp>
34
35 namespace YSLib
36 {
37
38 namespace UI
39 {
40
45 template<class _tCon>
46 class GSequenceViewer
47 {
48 public:
53 using ContainerType = _tCon;
54 using SizeType = typename _tCon::size_type;
55 using DifferenceType = typename _tCon::difference_type; \
57
59 static_assert(std::is_unsigned<SizeType>::value,
60 "Invalid size type found!");
62 static_assert(std::is_signed<DifferenceType>::value,
63 "Invalid difference type found!");
64
65 private:
71 _tCon* p_con;
72 SizeType head;
73 SizeType selected;
74 SizeType length;
75 bool is_selected;
76
77 public:
83 explicit
84 GSequenceViewer(ContainerType& con)
85 : p_con(&con), head(0), selected(0), length(1), is_selected(false)
86 {}
87
88 PDefHOp(GSequenceViewer&, ++, )
89 ImplRet(IncreaseSelected(1))
90 PDefHOp(GSequenceViewer&, --, )
91 ImplRet(IncreaseSelected(-1))
92 PDefHOp(GSequenceViewer&, ++, int) \
94 ImplRet(IncreaseHead(1))
95 PDefHOp(GSequenceViewer&, --, int) \
97 ImplRet(IncreaseHead(-1))
98
102 DefPred(const ynothrow, Selected, is_selected)
103
107 bool
108 Contains(SizeType i) const
109 {
110 return GetTotal() != 0 && GetLength() != 0
111 && IsInInterval<SizeType>(i - GetHeadIndex(), GetLength());
112 }
113
114 DefGetter(const ynothrow, SizeType, Total, p_con->size()) \
116 DefGetter(const ynothrow, SizeType, Length, length)
117 DefGetter(const ynothrow, SizeType, HeadIndex, head)
118 DefGetter(const ynothrow, SizeType, SelectedIndex, selected)
119 DefGetter(const ynothrow, DifferenceType, Offset, IsSelected()
120 ? GetSelectedIndex() - GetHeadIndex() : -1) \
122 DefGetter(const ynothrow, SizeType, Valid, min(GetTotal() - GetHeadIndex(),
123 GetLength()))
124
130 void
131 SetContainer(ContainerType& con)
132 {
133 if(YB_LIKELY(p_con != &con))
134 yunseq(p_con = &con, selected = 0, head = 0, length = 1);
135 }
139 bool
140 SetHeadIndex(SizeType t)
141 {
142 if(t < GetTotal() && t != head)
143 {
144 if(t == 0)
145 MoveViewerToBegin();
146 else if(length + t > GetTotal())
147 MoveViewerToEnd();
148 else
149 head = t;
150 return true;
151 }
152 return false;
153 }
157 bool
158 SetLength(SizeType l)
159 {
160 if(l != length)
161 {
162 length = l;
163 return true;
164 }
165 return false;
166 }
170 bool
171 SetSelectedIndex(SizeType t)
172 {
173 if(t < GetTotal() && !(t == selected && is_selected))
174 {
175 selected = t;
176 RestrictView();
177 is_selected = true;
178 return true;
179 }
180 return false;
181 }
182
190 bool
191 AdjustForContent()
192 {
193 const auto total(GetTotal());
194
195 if(total != 0)
196 {
197 if(!(selected < total))
198 selected = total - 1;
199 return RestrictView();
200 }
201 else
202 Reset();
203 return true;
204 }
205
209 PDefH(void, ClearSelected, )
210 ImplExpr(is_selected = {})
211
212 inline PDefH(GSequenceViewer&, DecreaseHead, DifferenceType d) \
214 ImplRet(IncreaseHead(-d))
215
216 inline PDefH(GSequenceViewer&, DecreaseSelected, DifferenceType d) \
218 ImplRet(IncreaseSelected(-d))
219
224 GSequenceViewer&
225 IncreaseHead(DifferenceType d)
226 {
227 int t(head + d);
228
229 RestrictInInterval(t, 0, int(GetTotal()));
230 SetHeadIndex(t);
231 return *this;
232 }
233
238 GSequenceViewer&
239 IncreaseSelected(DifferenceType d)
240 {
241 int t(selected + d);
242
243 RestrictInInterval(t, 0, int(GetTotal()));
244 SetSelectedIndex(t);
245 return *this;
246 }
247
251 bool
252 MoveViewerToBegin()
253 {
254 if(head)
255 {
256 head = 0;
257 return true;
258 }
259 return false;
260 }
261
265 bool
266 MoveViewerToEnd()
267 {
268 if(GetTotal() < length)
269 return false;
270 head = GetTotal() - length;
271 return true;
272 }
273
278 PDefH(void, Reset, )
279 ImplUnseq(head = 0, selected = 0, length = 1, is_selected = {})
280
287 bool
288 RestrictSelected()
289 {
290 if(GetTotal() == 0)
291 return false;
292 if(selected < head)
293 selected = head;
294 else if(selected < head + length)
295 return false;
296 else
297 selected = head + length - 1;
298 return true;
299 }
300
307 bool
308 RestrictView()
309 {
310 if(GetTotal() == 0)
311 return false;
312 if(selected < head)
313 head = selected;
314 else if(selected < head + length)
315 return false;
316 else
317 head = selected + 1 - length;
318 return true;
319 }
320 };
321
322 } // namespace UI;
323
324 } // namespace YSLib;
325
326 #endif
327
YSLib::UI::GSequenceViewer::DifferenceType
typename _tCon::difference_type DifferenceType
项目索引差值类型。
Definition: viewer.hpp:56
YSLib::UI::ClearSelected
ClearSelected
Definition: textlist.h:229
ImplUnseq
#define ImplUnseq(...)
Definition: YBaseMacro.h:107
YModules.h
YSLib::UI::GSequenceViewer::Selected
int int Selected
Definition: viewer.hpp:102
DefPred
#define DefPred(_q, _n,...)
Definition: YBaseMacro.h:172
YSLib::UI::GSequenceViewer::SelectedIndex
length SelectedIndex
Definition: viewer.hpp:118
YSLib::UI::GSequenceViewer::PDefHOp
PDefHOp(GSequenceViewer &,++,) ImplRet(IncreaseSelected(1)) PDefHOp(GSequenceViewer &
YSLib::UI::GSequenceViewer::ImplRet
ImplRet(IncreaseSelected(-1)) PDefHOp(GSequenceViewer &
< 选中项目的索引自减。
platform_ex::Windows::l
l
Definition: MinGW32.cpp:50
YSLib::UI::GSequenceViewer::SetSelectedIndex
bool SetSelectedIndex(SizeType t)
设置选中项目的索引。
Definition: viewer.hpp:171
YSLib::UI::GSequenceViewer::SetHeadIndex
bool SetHeadIndex(SizeType t)
设置视图中首个项目的索引。
Definition: viewer.hpp:140
YSLib::UI::GSequenceViewer::head
SizeType head
视图中首个项目的索引，大于等于 GetTotal() 时无效。
Definition: viewer.hpp:72
YSLib::UI::GSequenceViewer::min
length selected min(GetTotal()-GetHeadIndex(), GetLength())) void SetContainer(ContainerType &con)
< 取当前视图中有效项目个数。
Definition: viewer.hpp:122
YSLib::UI::PDefH
PDefH(void, Activate, Console &console, Drawing::Color fc=Drawing::ColorSpace::White) ImplExpr(Activate(console
激活：使用指定屏幕、有效性、前景色和默认背景色。
YSLib::UI::GSequenceViewer
序列视图类模板。
Definition: viewer.hpp:46
yunseq
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:748
ImplExpr
#define ImplExpr(...)
Definition: YBaseMacro.h:93
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
YSLib::UI::GSequenceViewer::selected
SizeType selected
选中项目的索引，大于等于 GetTotal() 时无效。
Definition: viewer.hpp:73
YSLib::UI::GSequenceViewer::Length
Length
Definition: viewer.hpp:116
YSLib::UI::GSequenceViewer::p_con
_tCon * p_con
序列容器指针。
Definition: viewer.hpp:60
YSLib::UI::GSequenceViewer::AdjustForContent
bool AdjustForContent()
按序列内容大小依次调整选中和首个项目的索引。
Definition: viewer.hpp:191
YSLib::UI::GSequenceViewer::SizeType
typename _tCon::size_type SizeType
项目索引类型。
Definition: viewer.hpp:54
YSLib::UI::GSequenceViewer::GSequenceViewer
GSequenceViewer(ContainerType &con)
构造：使用指定容器。
Definition: viewer.hpp:84
ytest::timing::total
auto total(size_t n, _fNow now, _fCallable &&f, _tParams &&...args) -> decltype(now()-now())
测试指定函数执行若干次的总时间。
Definition: timing.hpp:96
platform_ex::Reset
unsigned long Reset(COMPtr< _iCOM > &ptr) ynothrow
Definition: include/YCLib/COM.h:406
YSLib::UI::GSequenceViewer::is_selected
bool is_selected
选中状态。
Definition: viewer.hpp:75
YSLib::UI::GSequenceViewer::Valid
length selected Valid
Definition: viewer.hpp:122
YSLib::UI::GSequenceViewer::ContainerType
_tCon ContainerType
容器类型。
Definition: viewer.hpp:53
algorithm.hpp
泛型算法。
YSLib::RestrictInInterval
void RestrictInInterval(_type &i, int a, int b) ynothrow
约束整数 i 在左闭右开区间 [a, b) 中。
Definition: ycutil.h:283
YB_LIKELY
#define YB_LIKELY(expr)
Definition: ydef.h:297
YSLib::UI::GSequenceViewer::SetLength
bool SetLength(SizeType l)
设置长度。
Definition: viewer.hpp:158
YSLib::UI::GSequenceViewer::length
SizeType length
视图长度：最大可视项目数。
Definition: viewer.hpp:74
YSLib::UI::GSequenceViewer::Contains
int int is_selected bool Contains(SizeType i) const
判断是否在有效范围内包含指定项目索引。
Definition: viewer.hpp:108
YSLib::UI::GSequenceViewer::DefGetter
DefGetter(const ynothrow, SizeType, Total, p_con->size()) DefGetter(const ynothrow