FastReport

Форк
0
395 строк · 11.6 Кб
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.ComponentModel;
5
using System.Drawing;
6
using FastReport.Data;
7
using FastReport.Utils;
8

9
namespace FastReport.Table
10
{
11
    /// <summary>
12
    /// Represents a table row.
13
    /// </summary>
14
    /// <remarks>
15
    /// Use the <see cref="Height"/> property to set the height of a row. If <see cref="AutoSize"/>
16
    /// property is <b>true</b>, the row will calculate its height automatically.
17
    /// <para/>You can also set the <see cref="MinHeight"/> and <see cref="MaxHeight"/> properties
18
    /// to restrict the row's height.
19
    /// </remarks>
20
    public partial class TableRow : ComponentBase, IParent
21
    {
22
        #region Fields
23
        private List<TableCellData> cells;
24
        private float minHeight;
25
        private float maxHeight;
26
        private bool autoSize;
27
        private bool canBreak;
28
        private bool pageBreak;
29
        private int keepRows;
30
        private int index;
31
        private float saveHeight;
32
        private bool saveVisible;
33
        private bool serializingToPreview;
34
        #endregion
35

36
        #region Properties
37
        /// <summary>
38
        /// Gets or sets a height of the row, in pixels.
39
        /// </summary>
40
        /// <remarks>
41
        /// The row height cannot exceed the range defined by the <see cref="MinHeight"/> 
42
        /// and <see cref="MaxHeight"/> properties.
43
        /// <note>To convert between pixels and report units, use the constants defined 
44
        /// in the <see cref="Units"/> class.</note>
45
        /// </remarks>
46
        [TypeConverter("FastReport.TypeConverters.UnitsConverter, FastReport")]
47
        public override float Height
48
        {
49
            get { return base.Height; }
50
            set
51
            {
52
                value = Converter.DecreasePrecision(value, 2);
53
                if (value > MaxHeight && !canBreak)
54
                    value = MaxHeight;
55
                if (value < MinHeight)
56
                    value = MinHeight;
57
                base.Height = value;
58
            }
59
        }
60

61
        /// <summary>
62
        /// Gets or sets the minimal height for this row, in pixels.
63
        /// </summary>
64
        [DefaultValue(0f)]
65
        [Category("Layout")]
66
        [TypeConverter("FastReport.TypeConverters.UnitsConverter, FastReport")]
67
        public float MinHeight
68
        {
69
            get { return minHeight; }
70
            set { minHeight = value; }
71
        }
72

73
        /// <summary>
74
        /// Gets or sets the maximal height for this row, in pixels.
75
        /// </summary>
76
        [DefaultValue(1000f)]
77
        [Category("Layout")]
78
        [TypeConverter("FastReport.TypeConverters.UnitsConverter, FastReport")]
79
        public float MaxHeight
80
        {
81
            get { return maxHeight; }
82
            set { maxHeight = value; }
83
        }
84

85
        /// <summary>
86
        /// Gets or sets a value determines if the row should calculate its height automatically.
87
        /// </summary>
88
        /// <remarks>
89
        /// The row height cannot exceed the range defined by the <see cref="MinHeight"/> 
90
        /// and <see cref="MaxHeight"/> properties.
91
        /// </remarks>
92
        [DefaultValue(false)]
93
        [Category("Behavior")]
94
        public bool AutoSize
95
        {
96
            get { return autoSize; }
97
            set { autoSize = value; }
98
        }
99

100
        /// <summary>
101
        /// Gets or sets a value that determines if the component can break its contents across pages.
102
        /// </summary>
103
        [DefaultValue(false)]
104
        [Category("Behavior")]
105
        public bool CanBreak
106
        {
107
            get { return canBreak; }
108
            set { canBreak = value; }
109
        }
110

111
        /// <summary>
112
        /// Gets the index of this row.
113
        /// </summary>
114
        [Browsable(false)]
115
        public int Index
116
        {
117
            get { return index; }
118
        }
119

120
        /// <inheritdoc/>
121
        [Browsable(false)]
122
        public override float Top
123
        {
124
            get
125
            {
126
                TableBase table = Parent as TableBase;
127
                if (table == null)
128
                    return 0;
129

130
                float result = 0;
131
                for (int i = 0; i < Index; i++)
132
                {
133
                    result += table.Rows[i].Height;
134
                }
135
                return result;
136
            }
137
            set { base.Top = value; }
138
        }
139

140
        /// <summary>
141
        /// Gets or sets the cell with specified index.
142
        /// </summary>
143
        /// <param name="col">Column index.</param>
144
        /// <returns>The <b>TableCell</b> object.</returns>
145
        [Browsable(false)]
146
        public TableCell this[int col]
147
        {
148
            get
149
            {
150
                TableCellData cellData = CellData(col);
151
                TableCell cell = cellData.Cell;
152
                cell.SetParent(this);
153
                cell.SetValue(cellData.Value);
154
                return cell;
155
            }
156
            set
157
            {
158
                TableCellData cellData = CellData(col);
159
                cellData.AttachCell(value);
160
            }
161
        }
162

163
        /// <summary>
164
        /// Gets or sets the page break flag for this row.
165
        /// </summary>
166
        [Browsable(false)]
167
        public bool PageBreak
168
        {
169
            get { return pageBreak; }
170
            set { pageBreak = value; }
171
        }
172

173
        /// <summary>
174
        /// Gets or sets the number of rows to keep on the same page.
175
        /// </summary>
176
        [Browsable(false)]
177
        public int KeepRows
178
        {
179
            get { return keepRows; }
180
            set { keepRows = value; }
181
        }
182

183
        internal static float DefaultHeight
184
        {
185
            get { return (int)Math.Round(18 / (0.25f * Units.Centimeters)) * (0.25f * Units.Centimeters); }
186
        }
187
        #endregion
188

189
        #region IParent Members
190
        /// <inheritdoc/>
191
        public bool CanContain(Base child)
192
        {
193
            return child is TableCell;
194
        }
195

196
        /// <inheritdoc/>
197
        public void GetChildObjects(ObjectCollection list)
198
        {
199
            TableBase table = Parent as TableBase;
200
            if (table == null)
201
                return;
202

203
            for (int i = 0; i < table.Columns.Count; i++)
204
            {
205
                if (!serializingToPreview || table.Columns[i].Visible)
206
                    list.Add(this[i]);
207
            }
208
        }
209

210
        /// <inheritdoc/>
211
        public void AddChild(Base child)
212
        {
213
            // support deserializing the cells
214
            if (child is TableCell)
215
            {
216
                this[cells.Count] = child as TableCell;
217
                child.SetParent(this);
218
            }
219
        }
220

221
        /// <inheritdoc/>
222
        public void RemoveChild(Base child)
223
        {
224
        }
225

226
        private TableCellData FindCellData(TableCell cell)
227
        {
228
            foreach (TableCellData cellData in cells)
229
            {
230
                if (cellData.Cell == cell)
231
                    return cellData;
232
            }
233
            return null;
234
        }
235

236
        /// <inheritdoc/>
237
        public int GetChildOrder(Base child)
238
        {
239
            TableCellData cellData = FindCellData(child as TableCell);
240
            return cellData == null ? 0 : cells.IndexOf(cellData);
241
        }
242

243
        /// <inheritdoc/>
244
        public void SetChildOrder(Base child, int order)
245
        {
246
            TableCellData cellData = FindCellData(child as TableCell);
247
            if (cellData == null)
248
                return;
249

250
            int oldOrder = child.ZOrder;
251
            if (oldOrder != -1 && order != -1 && oldOrder != order)
252
            {
253
                if (order > cells.Count)
254
                    order = cells.Count;
255
                if (oldOrder <= order)
256
                    order--;
257
                cells.Remove(cellData);
258
                cells.Insert(order, cellData);
259
            }
260
        }
261

262
        /// <inheritdoc/>
263
        public void UpdateLayout(float dx, float dy)
264
        {
265
            TableBase table = Parent as TableBase;
266
            if (table == null)
267
                return;
268

269
            // update this row cells
270
            for (int i = 0; i < table.Columns.Count; i++)
271
            {
272
                this.CellData(i).UpdateLayout(dx, dy);
273
            }
274

275
            // update spanned cells that contains this row
276
            List<Rectangle> spanList = table.GetSpanList();
277
            foreach (Rectangle span in spanList)
278
            {
279
                if (Index > span.Top && Index < span.Bottom)
280
                    table[span.Left, span.Top].CellData.UpdateLayout(dx, dy);
281
            }
282

283
        }
284
        #endregion
285

286
        #region Public Methods
287
        /// <inheritdoc/>
288
        public override void Assign(Base source)
289
        {
290
            TableRow src = source as TableRow;
291
            MinHeight = src.MinHeight;
292
            MaxHeight = src.MaxHeight;
293
            AutoSize = src.AutoSize;
294
            KeepRows = src.KeepRows;
295
            CanBreak = src.CanBreak;
296

297
            base.Assign(source);
298
        }
299

300
        internal TableCellData CellData(int col)
301
        {
302
            while (col >= cells.Count)
303
            {
304
                cells.Add(new TableCellData());
305
            }
306

307
            TableCellData cellData = cells[col];
308
            cellData.Table = Parent as TableBase;
309
            cellData.Address = new Point(col, Index);
310
            return cellData;
311
        }
312

313
        internal void CorrectCellsOnColumnChange(int index, int correct)
314
        {
315
            if (correct == 1)
316
                cells.Insert(index, new TableCellData());
317
            else if (index < cells.Count)
318
                cells.RemoveAt(index);
319
        }
320

321
        internal void SetIndex(int value)
322
        {
323
            index = value;
324
        }
325

326
        /// <inheritdoc/>
327
        public override void Serialize(FRWriter writer)
328
        {
329
            TableRow c = writer.DiffObject as TableRow;
330
            serializingToPreview = writer.SerializeTo == SerializeTo.Preview;
331
            base.Serialize(writer);
332

333
            if (FloatDiff(MinHeight, c.MinHeight))
334
                writer.WriteFloat("MinHeight", MinHeight);
335
            if (FloatDiff(MaxHeight, c.MaxHeight))
336
                writer.WriteFloat("MaxHeight", MaxHeight);
337
            if (FloatDiff(Height, c.Height))
338
                writer.WriteFloat("Height", Height);
339
            if (AutoSize != c.AutoSize)
340
                writer.WriteBool("AutoSize", AutoSize);
341
            if (CanBreak != c.CanBreak)
342
                writer.WriteBool("CanBreak", CanBreak);
343

344
            if (Parent is TableResult)
345
            {
346
                // write children by itself
347
                SetFlags(Flags.CanWriteChildren, true);
348
                writer.SaveChildren = true;
349

350
                TableResult table = Parent as TableResult;
351
                foreach (TableColumn column in table.ColumnsToSerialize)
352
                {
353
                    TableCell cell = this[column.Index];
354
                    writer.Write(cell);
355
                }
356
            }
357
        }
358

359
        /// <inheritdoc/>
360
        public override void Clear()
361
        {
362
            base.Clear();
363
            foreach (TableCellData cell in cells)
364
            {
365
                cell.Dispose();
366
            }
367
            cells.Clear();
368
        }
369

370
        internal void SaveState()
371
        {
372
            saveHeight = Height;
373
            saveVisible = Visible;
374
        }
375

376
        internal void RestoreState()
377
        {
378
            Height = saveHeight;
379
            Visible = saveVisible;
380
        }
381
        #endregion
382

383
        /// <summary>
384
        /// Initializes a new instance of the <see cref="TableRow"/> class.
385
        /// </summary>
386
        public TableRow()
387
        {
388
            cells = new List<TableCellData>();
389
            maxHeight = 1000;
390
            Height = DefaultHeight;
391
            SetFlags(Flags.CanCopy | Flags.CanDelete | Flags.CanWriteBounds, false);
392
            BaseName = "Row";
393
        }
394
    }
395
}
396

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.