bool CWizFolderSelector::isSelectGroupFolder()
{
    QTreeWidgetItem* item = m_folderView->currentItem();

    if (item->type() > QTreeWidgetItem::UserType)
    {
        CWizCategoryViewItemBase* baseItem = dynamic_cast<CWizCategoryViewItemBase*>(item);
        if (!baseItem)
            return false;

        int nPermission = m_app.databaseManager().db(baseItem->kbGUID()).permission();
        if (nPermission > m_nMinPermission)
        {
            if (nPermission >= WIZ_USERGROUP_READER)
            {
                CWizMessageBox::warning(this, tr("Info"), tr("You have no permission to create note in this group!"));
            }
            else if (nPermission >= WIZ_USERGROUP_EDITOR)
            {
                CWizMessageBox::warning(this, tr("Info"), tr("You have no permission to create folder in this group!"));
            }
            return false;
        }
    }

    if (!m_bAcceptRoot)
        return item->type() == Category_GroupNoTagItem || item->type() == Category_GroupItem;

    return item->type() == Category_GroupRootItem || item->type() == Category_GroupNoTagItem
            || item->type() == Category_GroupItem;
}
WIZTAGDATA CWizFolderSelector::selectedGroupFolder()
{
    QTreeWidgetItem* item = m_folderView->currentItem();
    if (item->type() == Category_GroupItem)
    {
        CWizCategoryViewGroupItem* p = dynamic_cast<CWizCategoryViewGroupItem*>(item);
        if (p)
        {
            return p->tag();
        }
    }
    else if (item->type() == Category_GroupNoTagItem || item->type() == Category_GroupRootItem)
    {
        CWizCategoryViewItemBase* p = dynamic_cast<CWizCategoryViewItemBase*>(item);
        if (p)
        {
            WIZTAGDATA tag;
            tag.strKbGUID = p->kbGUID();
            return tag;
        }
    }

    WIZTAGDATA tag;
    return tag;
}
void CWizCategoryBaseView::contextMenuEvent(QContextMenuEvent * e)
{
    CWizCategoryViewItemBase* pItem = dynamic_cast<CWizCategoryViewItemBase*>(itemAt(e->pos()));
    if (!pItem)
        return;

    pItem->showContextMenu(this, mapToGlobal(e->pos()));
}
bool CWizCategoryBaseView::acceptDocument(const WIZDOCUMENTDATA& document)
{
    QList<QTreeWidgetItem*> items = selectedItems();
    if (items.empty())
        return false;

    CWizCategoryViewItemBase* pItem = dynamic_cast<CWizCategoryViewItemBase*>(items.first());
    if (!pItem)
        return false;

    return pItem->accept(m_dbMgr.db(document.strKbGUID), document);
}
QString CWizCategoryBaseView::selectedItemKbGUID()
{
    QList<QTreeWidgetItem*> items = selectedItems();
    if (!items.isEmpty()) {
        CWizCategoryViewItemBase* pItem = dynamic_cast<CWizCategoryViewItemBase*>(items.first());
        if (!pItem)
            return NULL;

        return pItem->kbGUID();
    }

    return NULL;
}
void CWizCategoryBaseView::getDocuments(CWizDocumentDataArray& arrayDocument)
{
    QList<QTreeWidgetItem*> items = selectedItems();
    if (items.empty())
        return;

    CWizCategoryViewItemBase* pItem = dynamic_cast<CWizCategoryViewItemBase*>(items.first());
    if (!pItem)
        return;

    if (pItem->kbGUID().isEmpty()) {
        return;
    }

    pItem->getDocuments(m_dbMgr.db(pItem->kbGUID()), arrayDocument);
}
void CWizNoteStyle::drawCategoryViewItem(const QStyleOptionViewItemV4 *vopt,
                                         QPainter *p, const CWizCategoryBaseView *view) const
{
    if (view->isDragHovered() && view->validateDropDestination(view->dragHoveredPos())) {
        QRect rect = view->visualItemRect(view->itemAt(view->dragHoveredPos()));
        p->setRenderHint(QPainter::Antialiasing, true);
        QPen pen;
        pen.setStyle(Qt::SolidLine);
//        pen.setCapStyle(Qt::RoundCap);
        pen.setColor(QColor("#3498DB"));
        pen.setWidth(1);
        p->setPen(pen);
        p->setBrush(Qt::NoBrush);
        rect.setWidth(rect.width() - 2);
        p->drawRect(rect);
    }

    CWizCategoryViewItemBase* pItem = view->categoryItemFromIndex(vopt->index);

    if (view->isHelperItemByIndex(vopt->index)) {
        if (NULL != dynamic_cast<const CWizCategoryViewSectionItem*>(pItem)) {
            QString str = vopt->text;
            QRect rc(vopt->rect);
            rc.setTop(rc.top() + 12);
            Utils::StyleHelper::drawSingleLineText(p, rc, str, Qt::AlignVCenter, Utils::StyleHelper::treeViewItemCategoryText(), p->font());
        }
        else if (NULL != dynamic_cast<const CWizCategoryViewLinkItem*>(pItem)) {
            QString str = vopt->text;
            QRect rc(vopt->rect);
            rc.setLeft(rc.left() + 16);
            Utils::StyleHelper::drawSingleLineText(p, rc, str, Qt::AlignVCenter, Utils::StyleHelper::treeViewItemLinkText(), m_fontLink);
        }

        return;
    }

    p->save();

    bool bSelected = vopt->state.testFlag(State_Selected);
    int nLeftMargin = 4;
    if (!vopt->icon.isNull()) {
        QRect iconRect = subElementRect(SE_ItemViewItemDecoration, vopt, view);
        iconRect.adjust(nLeftMargin, 0, nLeftMargin, 0);
        Utils::StyleHelper::drawTreeViewItemIcon(p, iconRect, vopt->icon, bSelected && (vopt->state & State_HasFocus));
    }

    QFont f;
    Utils::StyleHelper::fontNormal(f);

    QFont fontCount;
    Utils::StyleHelper::fontExtend(fontCount);

    QRect rcText = subElementRect(SE_ItemViewItemText, vopt, view);
    rcText.adjust(nLeftMargin * 2, 0, nLeftMargin * 2, 0);
    QString strCount = pItem->countString();

    QString strText = vopt->text;
    if (!strText.isEmpty()) {
        QColor colorText = Utils::StyleHelper::treeViewItemText(bSelected && (vopt->state & State_HasFocus));

        p->setPen(colorText);
        int right = Utils::StyleHelper::drawSingleLineText(p, rcText, strText, Qt::AlignVCenter, colorText, f);
        //
        rcText.setLeft(right + 4);
    }

    if (!strCount.isEmpty()) {
        QRect rcCount = rcText;
        rcCount.setTop(rcCount.top() + 1);  //add extra 1 pixel for vcenter / 2
        QColor colorCount = Utils::StyleHelper::treeViewItemTextExtend(bSelected && (vopt->state & State_HasFocus));
        Utils::StyleHelper::drawSingleLineText(p, rcCount, strCount, Qt::AlignVCenter, colorCount, fontCount);
    }

    p->restore();

    // FIXME: this is used for drawing additional badge, please merge it.
    view->categoryItemFromIndex(vopt->index)->draw(p, vopt);
}
void CWizNoteStyle::drawCategoryViewItem(const QStyleOptionViewItemV4 *vopt,
                                         QPainter *p, const CWizCategoryBaseView *view) const
{
    CWizCategoryViewItemBase* pItem = view->categoryItemFromIndex(vopt->index);

    if (view->isHelperItemByIndex(vopt->index)) {
        if (NULL != dynamic_cast<const CWizCategoryViewSectionItem*>(pItem)) {
            QString str = vopt->text;
            QRect rc(vopt->rect);
            rc.setTop(rc.top() + 12);
            Utils::StyleHelper::drawSingleLineText(p, rc, str, Qt::AlignVCenter, Utils::StyleHelper::treeViewItemCategoryText(), p->font());
        }
        else if (NULL != dynamic_cast<const CWizCategoryViewLinkItem*>(pItem)) {
            QString str = vopt->text;
            QRect rc(vopt->rect);
            rc.setLeft(rc.left() + 16);
            Utils::StyleHelper::drawSingleLineText(p, rc, str, Qt::AlignVCenter, Utils::StyleHelper::treeViewItemLinkText(), m_fontLink);
        }

        return;
    }

    p->save();

    bool bSelected = vopt->state.testFlag(State_Selected);

    if (!vopt->icon.isNull()) {
        QRect iconRect = subElementRect(SE_ItemViewItemDecoration, vopt, view);
        Utils::StyleHelper::drawTreeViewItemIcon(p, iconRect, vopt->icon, bSelected);
    }

    QFont f;
    Utils::StyleHelper::fontNormal(f);

    QFont fontCount;
    Utils::StyleHelper::fontExtend(fontCount);

    QRect rcText = subElementRect(SE_ItemViewItemText, vopt, view);
    QString strCount = pItem->countString();

    QString strText = vopt->text;
    if (!strText.isEmpty()) {
        QColor colorText = Utils::StyleHelper::treeViewItemText(bSelected);

        p->setPen(colorText);
        int right = Utils::StyleHelper::drawSingleLineText(p, rcText, strText, Qt::AlignVCenter, colorText, f);
        //
        rcText.setLeft(right + 4);
    }

    if (!strCount.isEmpty()) {
        QRect rcCount = rcText;
        rcCount.setTop(rcCount.top() + 1);  //add extra 1 pixel for vcenter / 2
        QColor colorCount = Utils::StyleHelper::treeViewItemTextExtend(bSelected);
        Utils::StyleHelper::drawSingleLineText(p, rcCount, strCount, Qt::AlignVCenter, colorCount, fontCount);
    }

    p->restore();

    // FIXME: this is used for drawing additional badge, please merge it.
    view->categoryItemFromIndex(vopt->index)->draw(p, vopt);
}