#include "FileFolderBar.h"
#include <QPainter>
#include <queue>
namespace Toolbox {
    File_Folder_Bar::File_Folder_Bar(QWidget *parent) : QWidget(parent) {
        //setAttribute(Qt::WA_OpaquePaintEvent);
    }
    File_Folder_Bar::~File_Folder_Bar() {
    }
    void File_Folder::draw(int index, QPainter *painter) {
        static QImage file_folder = QImage(":fileFolder.png");
        int x = 10 + 20 * deep;
        int y = 10 + 30 * index;
//        painter->drawImage(x, y, file_folder);
        painter->drawImage(0, 0, file_folder);
        painter->drawText(x + 20, y + 10, name);
    }
    void File_Folder_Bar::paintEvent(QPaintEvent *event) {
        QWidget::paintEvent(event);
        QPainter painter(this);
        int index = 0;
        for (File_Folder *&root: roots) {
            std::queue<File_Folder *> que;
            root->deep = 0;
            que.push(root);
            while (!que.empty()) {
                File_Folder *cur = que.front();
                que.pop();
                int n = cur->sons.size();
                cur->draw(index++, &painter);
                for (int i = 0; i < n; ++i) {
                    if (cur->is_show_sons) {
                        cur->sons[i]->deep = cur->deep + 1;
                        que.push(cur->sons[i]);
                    }
                }
            }
        }
        painter.end();
    }
}
