1 /*
2 * Copyright (c) 2020 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "view_group_page.h"
17 #include "ui_config.h"
18
19 namespace OHOS {
ViewGroupPage(UIViewGroup * viewGroup)20 ViewGroupPage::ViewGroupPage(UIViewGroup* viewGroup)
21 {
22 viewGroup_ = viewGroup;
23 }
24
~ViewGroupPage()25 ViewGroupPage::~ViewGroupPage()
26 {
27 ListNode<AppInfo*>* app = appInfo_.Begin();
28 while (app != appInfo_.End()) {
29 delete app->data_;
30 app = app->next_;
31 }
32 appInfo_.Clear();
33 if (row_col_) {
34 delete[] row_col_;
35 }
36 }
37
IsFull(int16_t & row,int16_t & col)38 bool ViewGroupPage::IsFull(int16_t& row, int16_t& col)
39 {
40 for (int16_t i = 0; i < row_; i++) {
41 for (int16_t j = 0; j < col_; j++) {
42 if (row_col_[i][j] == false) {
43 row = i;
44 col = j;
45 return false;
46 }
47 }
48 }
49 return true;
50 }
51
SetStyle(Style sty)52 void ViewGroupPage::SetStyle(Style sty)
53 {
54 viewGroup_->SetStyle(sty);
55 viewGroup_->Invalidate();
56 }
57
SetPosion(int16_t width,int16_t height,int16_t x,int16_t y)58 void ViewGroupPage::SetPosion(int16_t width, int16_t height, int16_t x, int16_t y)
59 {
60 viewGroup_->SetPosition(x, y, width, height);
61 }
62
SetScale(double scale)63 void ViewGroupPage::SetScale(double scale)
64 {
65 scale_ = scale;
66 }
67
SetMatrix(int16_t rows,int16_t cols)68 void ViewGroupPage::SetMatrix(int16_t rows, int16_t cols)
69 {
70 row_col_ = new bool* [rows];
71 for (int i = 0; i < rows; i++) {
72 row_col_[i] = new bool[cols]();
73 }
74 row_ = rows;
75 col_ = cols;
76 }
77
CalculateAppPosition(AppInfo * pAppInfo,int16_t row,int16_t col)78 void ViewGroupPage::CalculateAppPosition(AppInfo* pAppInfo, int16_t row, int16_t col)
79 {
80 int16_t w = viewGroup_->GetWidth();
81
82 const double scale = scale_;
83 const int16_t blank1 = 10;
84 const int16_t blank2 = 30;
85 const int16_t labelH = 2;
86 int16_t width = static_cast<int16_t>(static_cast<double>(w) / static_cast<double>(scale * col_ + col_ + scale));
87 int16_t heightB = width;
88 int16_t heightL = heightB / labelH;
89 int16_t xB = scale * width + (scale + 1) * width * col;
90 int16_t yB = blank1 + (blank2 + heightL + heightB) * row;
91 int16_t xL = xB;
92 int16_t yL = yB + heightB + blank1;
93
94 pAppInfo->buttonXY_.x = xB;
95 pAppInfo->buttonXY_.y = yB;
96 pAppInfo->buttonHV_.x = width;
97 pAppInfo->buttonHV_.y = heightB;
98
99 pAppInfo->lableXY_.x = xL;
100 pAppInfo->lableXY_.y = yL;
101 pAppInfo->lableHV_.x = width;
102 pAppInfo->lableHV_.y = heightL;
103 }
104
SetUpApp(AppInfo * pAppInfo)105 void ViewGroupPage::SetUpApp(AppInfo *pAppInfo)
106 {
107 UILabelButton *button = new UILabelButton();
108 UILabel *lable = new UILabel();
109 lable->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::Red()));
110 lable->SetStyle(STYLE_BACKGROUND_OPA, UN_OPACITY);
111 pAppInfo->SetButton(button);
112 pAppInfo->SetLable(lable);
113
114 pAppInfo->SetListener(pAppInfo);
115 viewGroup_->Add(button);
116 viewGroup_->Add(lable);
117 viewGroup_->Invalidate();
118 }
119
AddApp(AppInfo * pAppInfo)120 bool ViewGroupPage::AddApp(AppInfo* pAppInfo)
121 {
122 int16_t row = 0;
123 int16_t col = 0;
124
125 if (FindApp(pAppInfo)) {
126 return true;
127 }
128
129 if (IsFull(row, col)) {
130 return false;
131 }
132
133 pAppInfo->SetLocation(row, col);
134 CalculateAppPosition(pAppInfo, row, col);
135
136 SetUpApp(pAppInfo);
137 appInfo_.PushBack(pAppInfo);
138 row_col_[row][col] = true;
139 return true;
140 }
141
FindApp(AppInfo * pApp)142 bool ViewGroupPage::FindApp(AppInfo* pApp)
143 {
144 ListNode<AppInfo*>* app = appInfo_.Begin();
145 while (app != appInfo_.End()) {
146 if (memcmp(app->data_->appName_, pApp->appName_, strlen(pApp->appName_)) == 0) {
147 return true;
148 }
149 app = app->next_;
150 }
151 return false;
152 }
153
RemoveApp(const char * pAppName)154 bool ViewGroupPage::RemoveApp(const char* pAppName)
155 {
156 ListNode<AppInfo*>* app = appInfo_.Begin();
157 while (app != appInfo_.End()) {
158 if (memcmp(app->data_->appName_, pAppName, strlen(pAppName)) == 0) {
159 row_col_[app->data_->row_col_.x][app->data_->row_col_.y] = false;
160 viewGroup_->Remove(app->data_->button_);
161 viewGroup_->Remove(app->data_->lable_);
162 viewGroup_->Invalidate();
163 appInfo_.Remove(app);
164 return true;
165 }
166 app = app->next_;
167 }
168 return false;
169 }
170 } // namespace OHOS
171