// protected
void kpAbstractImageSelectionTool::changeImageSelectionTransparency (
        const QString &name,
        const kpImageSelectionTransparency &newTrans,
        const kpImageSelectionTransparency &oldTrans)
{
#if DEBUG_KP_TOOL_SELECTION
    qCDebug(kpLogTools) << "CALL(" << name << ")";
#endif

    kpSetOverrideCursorSaver cursorSaver (Qt::WaitCursor);

    if (hasBegunShape ())
        endShapeInternal ();

    kpAbstractImageSelection *imageSel = document ()->imageSelection ();

    if (imageSel->hasContent () && newTrans.isTransparent ())
        environ ()->flashColorSimilarityToolBarItem ();

    imageSel->setTransparency (newTrans);

    // We _must_ add the command even if kpAbstractImageSelection::setTransparency()
    // above did not change the selection transparency mask at all.
    // Consider the following case:
    //
    //   0. Ensure that selection transparency is opaque and any
    //      color other than red is the background color.  Ensure that
    //      the color similarity setting is 0.
    //
    //   1. Select a solid red rectangle and pull it off.
    //
    //   2. Switch to transparent and set red as the background color.
    //      [the selection is now invisible as red is the background
    //       color, which is the same as the contents of the selection]
    //
    //   3. Invert Colors.
    //      [the selection is now cyan, red is still the background color]
    //
    //   4. Change the background color to green.
    //      [the selection transparency mask does not change so the
    //       selection is still cyan; green is the background color]
    //
    //   5. Undo
    //
    // If no transparency command were added for Step 4., the Undo
    // in Step 5. would take us straight to the state after Step 2.,
    // where we would expect the red selection to be invisible.  However,
    // as the background color was changed to green in Step 4. and was not
    // undone, the red selection is not invisible when it should be -- Undo
    // has moved us to an incorrect state.
    //
    // KDE3: Copy this comment into the KDE 3 branch.
    commandHistory ()->addCommand (new kpToolImageSelectionTransparencyCommand (
        name,
        newTrans, oldTrans,
        environ ()->commandEnvironment ()),
        false/*no exec*/);
}
Ejemplo n.º 2
0
void showNothingToAutocropMessage (kpMainWindow *mainWindow, bool actOnSelection)
{
    kpSetOverrideCursorSaver cursorSaver (Qt::arrowCursor);

    if (actOnSelection)
    {
        KMessageBox::information (mainWindow,
                                  i18n ("KolourPaint cannot remove the selection's internal border as it"
                                        " could not be located."),
                                  i18n ("Cannot Remove Internal Border"),
                                  "NothingToAutoCrop");
    }
    else
    {
        KMessageBox::information (mainWindow,
                                  i18n ("KolourPaint cannot automatically crop the image as its"
                                        " border could not be located."),
                                  i18n ("Cannot Autocrop"),
                                  "NothingToAutoCrop");
    }
}
Ejemplo n.º 3
0
bool kpToolAutoCrop (kpMainWindow *mainWindow)
{
#if DEBUG_KP_TOOL_AUTO_CROP
    kdDebug () << "kpToolAutoCrop() CALLED!" << endl;
#endif

    if (!mainWindow)
    {
        kdError () << "kpToolAutoCrop() passed NULL mainWindow" << endl;
        return false;
    }

    kpDocument *doc = mainWindow->document ();
    if (!doc)
    {
        kdError () << "kpToolAutoCrop() passed NULL document" << endl;
        return false;
    }

    // OPT: if already pulled selection pixmap, no need to do it again here
    QPixmap pixmap = doc->selection () ? doc->getSelectedPixmap () : *doc->pixmap ();
    if (pixmap.isNull ())
    {
        kdError () << "kptoolAutoCrop() pased NULL pixmap" << endl;
        return false;
    }

    kpViewManager *vm = mainWindow->viewManager ();
    if (!vm)
    {
        kdError () << "kpToolAutoCrop() passed NULL vm" << endl;
        return false;
    }

    int processedColorSimilarity = mainWindow->colorToolBar ()->processedColorSimilarity ();
    kpToolAutoCropBorder leftBorder (&pixmap, processedColorSimilarity),
                         rightBorder (&pixmap, processedColorSimilarity),
                         topBorder (&pixmap, processedColorSimilarity),
                         botBorder (&pixmap, processedColorSimilarity);


    kpSetOverrideCursorSaver cursorSaver (Qt::waitCursor);

    // TODO: With Colour Similarity, a lot of weird (and wonderful) things can
    //       happen resulting in a huge number of code paths.  Needs refactoring
    //       and regression testing.
    //
    // TODO: e.g. When the top fills entire rect but bot doesn't we could
    //       invalidate top and continue autocrop.
    int numRegions = 0;
    if (!leftBorder.calculate (true/*x*/, +1/*going right*/) ||
            leftBorder.fillsEntirePixmap () ||
            !rightBorder.calculate (true/*x*/, -1/*going left*/) ||
            rightBorder.fillsEntirePixmap () ||
            !topBorder.calculate (false/*y*/, +1/*going down*/) ||
            topBorder.fillsEntirePixmap () ||
            !botBorder.calculate (false/*y*/, -1/*going up*/) ||
            botBorder.fillsEntirePixmap () ||
            ((numRegions = leftBorder.exists () +
                           rightBorder.exists () +
                           topBorder.exists () +
                           botBorder.exists ()) == 0))
    {
#if DEBUG_KP_TOOL_AUTO_CROP
        kdDebug () << "\tcan't find border; leftBorder.rect=" << leftBorder.rect ()
                   << " rightBorder.rect=" << rightBorder.rect ()
                   << " topBorder.rect=" << topBorder.rect ()
                   << " botBorder.rect=" << botBorder.rect ()
                   << endl;
#endif
        ::showNothingToAutocropMessage (mainWindow, (bool) doc->selection ());
        return false;
    }

#if DEBUG_KP_TOOL_AUTO_CROP
    kdDebug () << "\tnumRegions=" << numRegions << endl;
    kdDebug () << "\t\tleft=" << leftBorder.rect ()
               << " refCol=" << (leftBorder.exists () ? (int *) leftBorder.referenceColor ().toQRgb () : 0)
               << " avgCol=" << (leftBorder.exists () ? (int *) leftBorder.averageColor ().toQRgb () : 0)
               << endl;
    kdDebug () << "\t\tright=" << rightBorder.rect ()
               << " refCol=" << (rightBorder.exists () ? (int *) rightBorder.referenceColor ().toQRgb () : 0)
               << " avgCol=" << (rightBorder.exists () ? (int *) rightBorder.averageColor ().toQRgb () : 0)
               << endl;
    kdDebug () << "\t\ttop=" << topBorder.rect ()
               << " refCol=" << (topBorder.exists () ? (int *) topBorder.referenceColor ().toQRgb () : 0)
               << " avgCol=" << (topBorder.exists () ? (int *) topBorder.averageColor ().toQRgb () : 0)
               << endl;
    kdDebug () << "\t\tbot=" << botBorder.rect ()
               << " refCol=" << (botBorder.exists () ? (int *) botBorder.referenceColor ().toQRgb () : 0)
               << " avgCol=" << (botBorder.exists () ? (int *) botBorder.averageColor ().toQRgb () : 0)
               << endl;
#endif


    // In case e.g. the user pastes a solid, coloured-in rectangle,
    // we favour killing the bottom and right regions
    // (these regions probably contain the unwanted whitespace due
    //  to the doc being bigger than the pasted selection to start with).
    //
    // We also kill if they kiss or even overlap.

    if (leftBorder.exists () && rightBorder.exists ())
    {
        const kpColor leftCol = leftBorder.averageColor ();
        const kpColor rightCol = rightBorder.averageColor ();

        if ((numRegions == 2 && !leftCol.isSimilarTo (rightCol, processedColorSimilarity)) ||
                leftBorder.right () >= rightBorder.left () - 1)  // kissing or overlapping
        {
#if DEBUG_KP_TOOL_AUTO_CROP
            kdDebug () << "\tignoring left border" << endl;
#endif
            leftBorder.invalidate ();
        }
    }

    if (topBorder.exists () && botBorder.exists ())
    {
        const kpColor topCol = topBorder.averageColor ();
        const kpColor botCol = botBorder.averageColor ();

        if ((numRegions == 2 && !topCol.isSimilarTo (botCol, processedColorSimilarity)) ||
                topBorder.bottom () >= botBorder.top () - 1)  // kissing or overlapping
        {
#if DEBUG_KP_TOOL_AUTO_CROP
            kdDebug () << "\tignoring top border" << endl;
#endif
            topBorder.invalidate ();
        }
    }


    mainWindow->addImageOrSelectionCommand (
        new kpToolAutoCropCommand (
            (bool) doc->selection (),
            leftBorder, rightBorder,
            topBorder, botBorder,
            mainWindow));


    return true;
}